Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery getting the correct value based on checkbox checked

I cannot get this simple code to work. I want to get the value from the data attribute and based on that pass it to the alert

http://jsfiddle.net/btL9C/

$("input[type=checkbox]").change(function() {
     var section_list = $('.section').data('section_list');

     if ($(this).hasClass(section_list+"_list")) {          
         alert(section_list);
     }
});

html:

<input type="checkbox" data-section_list = "1" class="section 1_list">
<input type="checkbox" data-section_list = "2" class="section 2_list">
<input type="checkbox" data-section_list = "3" class="section 3_list">

How can I get the alert to display the corresponding value of data-section_list?

like image 343
Gadgetster Avatar asked Dec 11 '22 05:12

Gadgetster


1 Answers

Try this

$("input[type=checkbox]").change(function() {

    var section_list = $(this).data().section_list;

   if ($(this).hasClass(section_list+"_list")) {          
        alert(section_list);
   }
});
like image 138
Bharath Avatar answered Dec 21 '22 23:12

Bharath