0

what's wrong with my code, I don't get any values:

<script> $(document).ready(function() { $("input[type=checkbox][checked]").each(function(event){ var get = $("input[@name=\'checkbox_pref\']:checked").val(); $("#result").html("&id=" + get); }); }); </script> </head> <body> <input type="checkbox" name="checkbox_pref" value = "1"/> <input type="checkbox" name="checkbox_pref" value = "2"/> <input type="checkbox" name="checkbox_pref" value = "3"/> <div id="result">result ...</div> 
1
  • 1
    the way I see it, you're doing that function for the checkboxes that are checked on loading. are any of them automatically checked or something? Commented Aug 10, 2010 at 15:03

3 Answers 3

6

Depends what you're trying to do, but it should be more along the lines of:

$(document).ready(function() { $("input[type=checkbox]").change(function(event){ $("#result").html("&id=" + this.value); }); }); 
Sign up to request clarification or add additional context in comments.

2 Comments

I agree, although I would do $('input:checkbox')
Thanks, but how get all selected values, no't just last?
2

You don't need to escape the single quotes or the @ symbol. Use this line:

$(document).ready(function() { $("input[type=checkbox][checked]").each(function(event){ var get = $("input[name='checkbox_pref']:checked").val(); $("#result").html("&id=" + get); }); }); 

2 Comments

There's nothing wrong with escaping the single quotes. EDIT: I see you updated your answer to remove the @ symbol.
.. I just checked your solution on jsFiddle.net, and it doesn't seem to be working. jsfiddle.net/cxhVV
0

Having just been checking a very similar type of functionality myself, here is my test. Just as a footnote FYI ;)

http://jsfiddle.net/7Tdgw/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.