1

now i get value if i selected checkbox(id which was selected), but how get value when i unselected (id which was unselected)?

For example I push on 1, and i get value 1. When I unselect 1, i get value 1. I hope you understand my problem.

<script>
$(document).ready(function()
{
    $("input[type=checkbox]").change(function(event){
        $("#result").html("&id=" + this.value);
    });
});
</script>
</head>                                                                 
<body>                                                                
<input type="checkbox" name="checkbox_pref" value = "1"/>1
<input type="checkbox" name="checkbox_pref" value = "2"/>2
<input type="checkbox" name="checkbox_pref" value = "3"/>3 
<div id="result">result ...</div>

2 Answers 2

1

I simply create an array with all the checked values.

Then i can use join to create string out of it for later use.

$(document).ready(function() {
    var $inputs = $("input[type=checkbox]");
    $inputs.change(function(event){
        var list = [];
        $inputs.filter(':checked').each(function() {
            list.push($(this).val());
        });
        $("#result").html("&id=" + list.join(",")); // Produces: &id = 1,2,3 (If they were all checked)
    });
});

Sign up to request clarification or add additional context in comments.

2 Comments

.. I just tried this and it didn't work. jsfiddle.net/8nhYq. Are you sure this works?
Thanks, I fixed the syntax errors, try: jsfiddle.net/8nhYq/1, post updated with valid JS
0

I believe if you use this.attr('checked') you should get back a boolean.

or you could do

this.is(':checked')

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.