JQuery element id

I know there is a plugin named .hasClass ();

I have the following

$('#page_background, #header_background').ColorPicker({...

how would I check if the page_background button is pressed, and not the header_background?

This is what I have now, it won’t work.

$('#page_background, #header_background').ColorPicker({
        onSubmit: function(hsb, hex, rgb, el) {
            $(el).val(hex);
            $(el).ColorPickerHide();

            var id = this.id;

            if(id == 'page_background')
                $('body').css("background-color","#"+hex);
        },
        onBeforeShow: function () {
            $(this).ColorPickerSetColor(this.value);
        }
    })
    .bind('keyup', function(){
        $(this).ColorPickerSetColor(this.value);
    });
+5
source share
2 answers
$(function(){
   $('#page_background, #header_background').click(function(){

   var id = this.id;
   if(id == 'page_background')
     // page background
   else
     //header 
 });
});

Working violin

Since you are using this colorpicker function inside onSubmit

onSubmit: function(hsb, hex, rgb, el) {

where you get the element like el, so use to iduse

 var id = $(el).attr('id');
 // or
 var id = $(el).prop('id'); // new way
 //or simply
 var id = el.id; // this should work too
+8
source

Assuming you have a link to the clicked item stored in this, you can simply use the native DOM property:

var id = this.id;

If thisis a jQuery object and not a DOM node, you can use this.attr("id").

, hasClass - jQuery, . jQuery.

+4

All Articles