user319854
user319854

Reputation: 4096

jQuery: checkbox

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>

Upvotes: 0

Views: 381

Answers (3)

David Yell
David Yell

Reputation: 11855

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

http://jsfiddle.net/7Tdgw/

Upvotes: 0

Tim Rogers
Tim Rogers

Reputation: 21713

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);
    });
});

Upvotes: 6

Matthew Jones
Matthew Jones

Reputation: 26190

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);
    });
});

Upvotes: 2

Related Questions