going crazy. Simple alert when checkbox is checked with jquery

livinzlife picture livinzlife · Aug 22, 2011 · Viewed 9.2k times · Source

I cannot get a simple alert to fire when a checkbox is clicked/checked. I have been scratching my head wondering why I cant get it to work and I know it is going to be something simple... So, what am I doing wrong?

<script type="text/javascript">
$('#test1').click(function(){
alert('clicked');
});
</script>

<input type="checkbox" id="test1" value="test1" name="test1" value="-1">test1</input>

Answer

Joseph Silber picture Joseph Silber · Aug 22, 2011

You are not waiting for DOM ready. If you will, it'll work:

<script type="text/javascript">
$(function(){
    $('#test1').click(function(){
        alert('clicked');
    });
});
</script>

<input type="checkbox" id="test1" value="test1" name="test1" value="1" /> test1

Fiddle: http://jsfiddle.net/sanbc/