tags:

views:

57

answers:

3

Hi,

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  A: 

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);
    });
});
Matthew Jones
There's nothing wrong with escaping the single quotes. EDIT: I see you updated your answer to remove the `@` symbol.
patrick dw
@Matthew... I just checked your solution on jsFiddle.net, and it doesn't seem to be working. http://jsfiddle.net/cxhVV/
Hristo
+6  A: 

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);
    });
});
Tim
I agree, although I would do `$('input:checkbox')`
DavidYell
Thanks, but how get all selected values, no't just last?
A: 

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

http://jsfiddle.net/7Tdgw/

DavidYell