简体   繁体   中英

jQuery color picker change color on click

I have a jQuery color picker inputs I want to have one main input that will be a parent to other children inputs and I want parent input to change children inputs value and background color when I click a button. I created a function that change value of children inputs when there is a parrent input value set but I can't figure out how to change children input background color for same color that is parent input.

I know that I have some errors becouse when I click on a button I have an error message

Cannot read property 'target' of undefined

JS

 function MainBackgroundColorChange(event) {
    $(".background-color-features").css({
        "background-color": $(event.target).css('background-color'),
        "color": $(event.target).css('color')
    });
}

// Dla tła
$('#background-color-main-button').on('click', function () {
    $('.background-color-features').val($('#background-color-main').val());
    MainBackgroundColorChange();
});


$('.show-hide-color-section-background').on('click', function () {
    if ($('.color-section-background').is(":visible")) {
        $('.color-section-background').hide("slide");
        $('.show-hide-color-section-background').text('Pokaż więcej');
    } else {
        $('.color-section-background').show("slide");
        $('.show-hide-color-section-background').text('Ukryj');
    }
});

HTML/BLADE.PHP

<div class="row main-header">
            <div class="col-sm-9">
                <a href="#" class="page-header" style="color:black">Kolory tła głównych elementów</a>
            </div>
        </div>
        <br>
        <div class="form-group">
            <label for="background_color_for_all" class="col-sm-2 control-label">Ustaw kolor tła dla wszystkich
                głównych elementów</label>
            <div class="col-sm-6 color-pick">
                {{Form::text('background_color_for_all', null, array('id' => 'background-color-main', 'class' => 'form-control bpm-colorpicker'))}}
            </div>
        </div>
        <div class="color-section-background" style="display: none">
            <div class="form-group">
                <label for="background_color_subpage_header" class="col-sm-2 control-label">Kolor tła w nagłówkach
                    podstron</label>
                <div class="col-sm-6 color-pick">
                    {{Form::text('background_color_subpage_header', null, array( 'class' => 'form-control bpm-colorpicker background-color-features'))}}
                </div>
            </div>

            <div class="form-group">
                <label for="background_color_objects" class="col-sm-2 control-label">Kolor tła obiektów (np. listy
                    ofert, wyszukiwarki ofert, nawigacji
                    stron)</label>
                <div class="col-sm-6">
                    {{Form::text('background_color_objects', null, array('class' => 'form-control bpm-colorpicker background-color-features'))}}
                </div>
            </div>


            <div class="form-group">
                <label for="background_color_header" class="col-sm-2 control-label">Kolor tła nagłówka</label>
                <div class="col-sm-6">
                    {{Form::text('background_color_header', null, array( 'class' => 'form-control bpm-colorpicker background-color-features'))}}
                </div>
            </div>


            <div class="form-group">
                <label for="background_color_menu_footer" class="col-sm-2 control-label">Kolor tła dolnego
                    menu</label>
                <div class="col-sm-6 color-pick">
                    {{Form::text('background_color_menu_footer', null, array( 'class' => 'form-control bpm-colorpicker background-color-features'))}}
                </div>
            </div>

            <div class="form-group">
                <label for="background_color_footer" class="col-sm-2 control-label">Kolor tła stopki</label>
                <div class="col-sm-6">
                    {{Form::text('background_color_footer', null, array( 'class' => 'form-control bpm-colorpicker background-color-features'))}}
                </div>
            </div>
        </div>
        <div class="form-group">
            <span class="col-sm-2"></span>
            <div class="col-sm-6">
                <button type="button" id="background-color-main-button" class="btn btn-seccondary">Ustaw dla
                    wszystkich
                </button>
            </div>
        </div>
        <div class="form-group">
            <span class="col-sm-2"></span>
            <div class="col-sm-6">
                <button type="button" class="btn btn-seccondary show-hide-color-section-background">
                    Pokaż więcej
                </button>
            </div>
        </div>

Get rid of that templating code or paste up what the rendered result is. That is probably the first problem.

 function MainBackgroundColorChange(event) {
$(".background-color-features").css({
    "background-color": $(event.target).css('background-color'),
    "color": $(event.target).css('color')
});

}

The target being elements with the class .background-color-features , don't exist until your template code has run, which could be why you are getting Cannot read property 'target' of undefined , is your templating code rendering properly? Lets see the result.

Here is a fiddle to fork from. https://jsfiddle.net/7hjkuzjt/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM