Reputation: 38695
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
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
Reputation: 19344
Use $.fn.live ...
$('input:checkbox[name=drawingNo]').live('click',function(){
alert('I am here');
});
Upvotes: 1
Reputation: 467
try removing ':checkbox' so that it looks like:
$('input[name=drawingNo]').click(function(){alert('I am here');});
any better?
Upvotes: 0