4

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?

7 Answers 7

4

Try this

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

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

   if ($(this).hasClass(section_list+"_list")) {          
        alert(section_list);
   }
});
Sign up to request clarification or add additional context in comments.

2 Comments

using 'this' in my actual code get's undefined even though the set up is the same as the one I posted
FIDDLE i have did the same thing. it seems to be working fine for me
2

Try to use $(this) reference while fetching the data from the current element,

$("input[type=checkbox]").change(function () {
    var section_list = $(this).data('section_list');
    if ($(this).hasClass(section_list + "_list")) {
        alert(section_list);
    }
}); //-- You have missed to mention the close parenthesis here.

DEMO

3 Comments

@Downvote: Can you please specify the reason for your vote..? so that i could fix the mistakes that i made..
@sudharsan .it s comedy dont take any serious .just fun
@RajaprabhuAravindasamy and bala please delete all comment which you are posted
2

you need to get current clicked element data attribute by using this currently you are getting using $(".section") which will not give desired output as there are multiple elements on page with this class:

change:

var section_list = $(".section").data('section_list');

to:

var section_list = $(this).data('section_list'); //<--- gets current cliked element data attribute

Your code will look like:

$("input:checkbox").change(function() {
     var section_list = $(this).data('section_list');

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

UPDATED FIDDLE

Comments

2

Demo

  1. Missing paranthesis last line - change function.
  2. Also use $(this) to get data.

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

         if ($(this).hasClass(section_list+"_list")) {          
             alert(section_list);
         }
    });  // <-- Missing
});

2 Comments

1. my mistake copying. 2. this gets me undefined value for my actual code which is similar set up
@Gadgetster, have you included jQuery reference?
1

You need to get the attribute of the element you are clicking on

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

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

3 Comments

I don't understand why people unecessarily try to downvote for no reason. I'm trying to contribute too.
it is wrong man - $(this).attr('data-section_list'); but i didn't put downvote for you thanks
why is that, is the attr (api.jquery.com/attr) depreciated? I know the data function is used to pass info that is not necessarily set as an attribute
1

Try this: use this instead .section after change function

<script type="text/javascript">
$(document).ready(function() {

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

     if ($(this).hasClass(section_list+"_list")) {          
         alert(section_list);  
     }
});  
});  
</script> 

http://jsfiddle.net/btL9C/3/

Comments

1

Try this:-

$("input[type=checkbox]").click( function(){
   if( $(this).is(':checked') ) alert($(this).attr("data-section_list"));
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.