Quantcast
Channel: Getting id of an element in jQuery using a function - Stack Overflow
Viewing all articles
Browse latest Browse all 6

Getting id of an element in jQuery using a function

$
0
0

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();        });

Viewing all articles
Browse latest Browse all 6

Latest Images

Trending Articles





Latest Images