momoterraw
momoterraw

Reputation: 164

JQuery how to bind on click event to all check box

I have multiple checkbox like so

<input name="1[]" type="checkbox">
<input name="1[]" type="checkbox">
<input name="2[]" type="checkbox">
<input name="2[]" type="checkbox">
<input name="3[]" type="checkbox">
<input name="4[]" type="checkbox">

How would I bind an onclick even to all of those checkbox. And run a function.. lets say a prompt "you click one of the listed checkbox"

 $("input").click() {

            return confirm("you click one of the listed checkbox?");

    });

This doesn't work

Upvotes: 2

Views: 10310

Answers (2)

Anujith
Anujith

Reputation: 9370

Use .change() for checkboxes, radiobuttons etc..

$("input[type=checkbox]").change(function() {
        return confirm("you click one of the listed checkbox?");
});

However your error was that you missed to make it a function of click

$("input[type=checkbox]").click(function() {
        return confirm("you click one of the listed checkbox?");
});

Upvotes: 1

tymeJV
tymeJV

Reputation: 104785

You need to make it a function of click

$('input[type="checkbox"]').click(function() {
    alert("you click one of the listed checkbox?");
});

Fiddled: http://jsfiddle.net/XQDfP/

Upvotes: 5

Related Questions