on click not working code example
Example 1: document on click not working
$(document).ready(function() {
$(document).on("click","#test-element",function() {
alert("click bound to document listening for #test-element");
});
$("#test-element").on("click",function() {
alert("click bound directly to #test-element");
});
$('body').append('<div id="test-element">Click mee</div>');
});
Example 2: html onclick not working
Note to other developers coming across this, you can run into this if you use a reserved method names e.g. clear.
<!DOCTYPE html>
<html>
<body>
<button onclick="clear()">Clear</button>
<button onclick="clear2()">Clear2</button>
<script>
function clear() {
alert('clear');
}
function clear2() {
alert('clear2');
}
</script>
</body>
</html>