0

I'm currently trying to get the values of checked checkboxes. These checkboxes have unique IDs because they are defined on a modal box.

<input type = 'checkbox' id = 'audience_Name-$row[asset_ID]' value = 'SMB'> SMB
<input type = 'checkbox' id = 'audience_Name-$row[asset_ID]' value = 'SMB'> SMB
<input type = 'checkbox' id = 'audience_Name-$row[asset_ID]' value = 'SMB'> SMB
<input type = 'checkbox' id = 'audience_Name-$row[asset_ID]' value = 'SMB'> SMB
<input type = 'checkbox' id = 'audience_Name-$row[asset_ID]' value = 'SMB'> SMB
<input type = 'button' id = 'editAssetColumn' value = 'Submit'>

And here, I'm testing if I'm retrieving them but unfortunately, I cannot.

$("button#editAssetColumn").click( function() {
    var edit_id = $(this).attr('id');
    var name = $("input#audience_Name:checked-"+edit_id).val();
    $('input#audience_Name:checked-'+edit_id).each(function() {
        alert("Success!");
    }); 
});

I believe it's the syntax... Thanks in advance!

4
  • protip: this.id > $(this).attr("id");. Also, don't use alert() as a debugging tool. console.log() is made just for that. Commented Sep 3, 2014 at 2:38
  • Hmm okay thanks for the suggestion. But my problem right now is syntax. I'm not really sure if I'm retrieving all the checked checkboxes with these codes. Specifically this line: "$('input#audience_Name:checked-'+edit_id).each(function() {". Am I getting all the checked checkboxes which are defined with an ID? Commented Sep 3, 2014 at 2:41
  • Cant get some logics #1 alert(audience_Name) where is 'audience_Name' defined. #2 $("input#audience_Name:checked-"+edit_id) does not make sense Commented Sep 3, 2014 at 2:41
  • Sorry, don't mind it. It's a string now. Commented Sep 3, 2014 at 2:43

3 Answers 3

1

Here is html and js, there were multiple problems in your code, JSFIDDLE here

HTML

<input type = 'checkbox' id = 'audience_Name[]-$row[1]' class="audience_Name" value = 'SMB'> SMB
<input type = 'checkbox' id = 'audience_Name[]-$row[2]' class="audience_Name" value = 'SMB'> SMB
<input type = 'checkbox' id = 'audience_Name[]-$row[3]' class="audience_Name" value = 'SMB'> SMB
<input type = 'checkbox' id = 'audience_Name[]-$row[4]' class="audience_Name"  value = 'SMB'> SMB
<input type = 'checkbox' id = 'audience_Name[]-$row[5]'  class="audience_Name"  value = 'SMB'> SMB
<input type = 'button' id = 'editAssetColumn' value = 'Submit'>

JS

$(document).ready(function(){
  $("#editAssetColumn").click( function() {
    console.log("here");
    $('.audience_Name:checked').each(function() {
        console.log("asdfsf");
    }); 
  });
});
Sign up to request clarification or add additional context in comments.

1 Comment

jsfiddle.net/km2vLfvz I added a this.id instead of asdfsf. But this is probably what he is after.
0

Your id is in the wrong format. it should be "input#audience_Name-" + edit_id + ":checked". since you are using the jQuery :checked selector.

1 Comment

In terms of syntax, this is what I'm looking for. Thank you! :)
0

Here you go. I'm using console.log() because I'm not a fan of getting a bazillion popups… But other than that, it's the same thing.

$("#editAssetColumn").on("click", function() {
  $('input:checkbox:checked').each(function() {
    console.log($(this).val());
  });
});

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.