[Answer]-Debugging javascript buttons

0👍

wrap the code in $(document).ready() method. ex,

$(document).ready(function(){
       $('#add_more').click(function() {
        alert('click');
    });
});

the functions inside .ready() method initializes when the whole document is ready.

👤Raab

1👍

You should always use the jQuery ready function to ensure the document is loaded before the code is executed. Eg:

$(function() {
    $('#add_more').click(function() {
        alert('click');
    });
});

0👍

You might need to wrap that in this:

$(document).ready(function () {
    $('#add_more').click(function() {
        alert('click');
    });
});

http://jsfiddle.net/HF5Bd/

👤xivo

0👍

1) your second script is loading before the DOM. You need to use document.ready

$(document).ready(function(){
    $('#add_more').click(function() {
     alert('click');     
    });
 }); 

2) another option is to move

<script>
    $('#add_more').click(function() {
     alert('click');     
    }); 
</script>  

below

<input type="button" value="Button 2" id="add_more">

Leave a comment