I have a simple code with two buttons in a div container. I want to get the id of a button using a function.
Problem: Can someone please explain why clicking on Button1 doesn't alert myButton1
? How can I solve this issue?
Code:
<html><body><style type="text/CSS"> #mydiv{ border: 1px dashed blue; width: 100px; height: 100px; }</style><div id="myDiv"><input type="button" id="myButton1" value="Button1"></input><input type="button" id="myButton2" value="Button2"></input></div><script type="text/javascript" src="jquery.js"></script><script type="text/javascript" src="code.js"></script></body></html>
code.js :
myfunction = function(e){ alert(this.id); e.stopPropagation(); }$('#myDiv').click(function(){ alert($('#myDiv').attr('id')); });$('#myButton1').click("myfunction()");$('#myButton2').click(function(e){ alert(this.id); e.stopPropagation(); });