views:

74

answers:

4

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(){...})

A: 

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

jessegavin
A: 

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

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

any better?

kaelle
just updated my post
Harry Pham
+1  A: 

Use $.fn.live ...

$('input:checkbox[name=drawingNo]').live('click',function(){
   alert('I am here');
});
Tracker1
which is exactly what I did in my edited post. tyvm :)
Harry Pham
+2  A: 

Your selector is wrong..

The below is the best format

$(document).ready(function(){
 $("input[name='drawingNo']").live('click', function(){
 alert('I am here');
 });            
});
joberror
which is exactly what I did in my edited post. tyvm :)
Harry Pham
check the way the selector is formatted " $("input[name='drawingNo']") " and the live event added
joberror