Karthik_SD
Karthik_SD

Reputation: 633

get parent element ID of a spectrum color picker

i have 2 color picker inside 2 different div.

<div class="panel-heading" style="width:100%" id="h1">
<span style="float:right; padding-right:10px;" class="close"> <input type='text' class="basic" id="t1"/> </span>
</div>

<div class="panel-heading" style="width:100%" id="h2">
<span style="float:right; padding-right:10px;" class="close"> <input type='text' class="basic" id="t2"/> </span>
</div>

and this is my spectrum chart picker JS:

$(document).ready(function () {
            $(".basic").spectrum({
                color: "rgb(244, 204, 204)",
                showPaletteOnly: true,
                hideAfterPaletteSelect: true,
                change: function (color) 
                {
                    setBackgroundColor(color);
                },
                palette: [
                    ["rgba(168, 255, 102, 0.29)", "rgb(67, 67, 67)", "rgb(102, 102, 102)",
                    "rgb(204, 204, 204)", "rgb(217, 217, 217)", "rgb(255, 255, 255)"],
                    ["rgb(152, 0, 0)", "rgb(255, 0, 0)", "rgb(255, 153, 0)", "rgb(255, 255, 0)", "rgb(0, 255, 0)",
                    "rgb(0, 255, 255)", "rgb(74, 134, 232)", "rgb(0, 0, 255)", "rgb(153, 0, 255)", "rgb(255, 0, 255)"]
                    ]
            });
});

Now inside setBackgroundColor() function how can i get to know which color picker div i have selected.(i am trying to change the background color of div) i.e H1 or H2.

Note : i dont want to send input ID in jquery as $("#t1").spectrum.

Please help me. Thanks in advance.

Upvotes: 2

Views: 1059

Answers (3)

Karthik_SD
Karthik_SD

Reputation: 633

Thanks everyone for your help.

this is how to change its parent bgcolor :

change: function (color) 
{
   $(this).parent().parent().css('background',color);
},

Upvotes: 0

Web Dev Guy
Web Dev Guy

Reputation: 1789

Maybe this will work for you.

$("#h1").focus(function() {
    //code when div #h1 focused
});

$("#h2").focus(function() {
    //code when div #h2 focused
});

or

this 

with focus function as Norlihazmey Ghazali stated

Upvotes: 1

Norlihazmey Ghazali
Norlihazmey Ghazali

Reputation: 9060

You can use this, but this will capture the input element because the plugin attacted to .basic element. To get the parent itself just use .parent() or .closest() :

$(".basic").spectrum({
     color: "rgb(244, 204, 204)",
     showPaletteOnly: true,
     hideAfterPaletteSelect: true,
     change: function (color)  {
         // use this
         console.log(this); // will show <input type='text' class="basic" id="t1"/>
         // to access parent element use .closest
         console.log( $(this).closest('.panel-heading') );
         // or
         console.log( $(this).closest('.close') );
         setBackgroundColor(color);
     },                
});

Upvotes: 2

Related Questions