Thang Pham
Thang Pham

Reputation: 38695

Detect checkbox has been click with JQuery?

What wrong with the my code. When I click on the checkbox, nothing happen

$(document).ready(function(){
     $('input:checkbox[name=drawingNo]').click(function(){alert('I am here');});            
});  
...
<body>
    <form>
        <input type="checkbox" name="drawingNo" value="1"> 1 <br>
        <input type="checkbox" name="drawingNo" value="2"> 2 <br>
        <input type="checkbox" name="drawingNo" value="3"> 3 <br>
        <input type="checkbox" name="drawingNo" value="4"> 4 <br>
    </form>
</body>

EDIT: The above code worked fine. What happen to me is that, the tag <input type="checkbox"> are generated by some other script, so when document.ready() fire up, it cant register click event to checkbox, since these checkbox are not really there yet. So to fixed it: change .click() into .live('click', function(){...})

Upvotes: 2

Views: 4322

Answers (4)

Joberror
Joberror

Reputation: 5890

Your selector is wrong..

The below is the best format

$(document).ready(function(){
 $("input[name='drawingNo']").live('click', function(){
 alert('I am here');
 });            
});

Upvotes: 4

Tracker1
Tracker1

Reputation: 19344

Use $.fn.live ...

$('input:checkbox[name=drawingNo]').live('click',function(){
   alert('I am here');
});

Upvotes: 1

kaelle
kaelle

Reputation: 467

try removing ':checkbox' so that it looks like:

$('input[name=drawingNo]').click(function(){alert('I am here');});

any better?

Upvotes: 0

jessegavin
jessegavin

Reputation: 75690

It works fine for me in IE, FF and Chrome.

Upvotes: 1

Related Questions