So , I have been scratching my head since long, to get the ID of the clicked button ,but couldn't get it right. It always returns me undefined
.
here's the fiddle : http://jsfiddle.net/thinkinbee/mx0rb5cy/
Is there something that i haven't included in my fiddle ? . Is there something wrong in my JS code ?
I took reference of previous questions but nothing seemed to work . I tried to implement both $(this).attr("id")
and this.id
but there was no positive result .
Also in the longer go all my buttons would be coming dynamically .any thing extra i will need to handle then ?
If the buttons are dynamic you should use document.on.
I got your fiddle to work like this: http://jsfiddle.net/1ehy66k9/
$(document).on('click', '.ui-btn', function() {
alert("Clicked with Id "+$(this).attr('id'));
});
You should pass this
as a parameter:
<a class="ui-btn ui-btn-inline ui-mini" id="check" onclick="chkAlert(this)">click me</a>
var chkAlert = function(elem){
alert("Clicked with Id "+$(elem).attr('id'));
alert("Clicked with Id "+elem.id);
};
Try this -
HTML -
<a class="ui-btn ui-btn-inline ui-mini" id="check" onclick="chkAlert(this)">click me</a>
JavaScript -
var chkAlert = function(t){
alert("Clicked with Id "+$(t).attr('id'));
};
you need to send the button into the function as variable this
:
<a class="ui-btn ui-btn-inline ui-mini" id="check" onclick="chkAlert(this)">click me</a>
and then receive it inside the function (you can name it whatever you want inside the function)
var chkAlert = function(caller){
alert("Clicked with Id "+caller.id);
};
you can pass the id to the function like onclick="chkAlert(this.id)" , please try as shown
<a class="ui-btn ui-btn-inline ui-mini" id="check" onclick="chkAlert(this.id)">click me</a>
var chkAlert = function(id){
alert("Clicked with Id "+id);
alert("Clicked with Id "+id);
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.