Getting the id of the pressed button
So, I scratched my head for a long time to get the id of the button pressed, but couldn't get it right. He always brings me back undefined
.
here's a 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 have addressed the previous questions, but nothing worked. I tried to implement both $(this).attr("id")
and this.id
, but there was no positive result.
Also on a longer stroke all my buttons will dynamically appear. Anything else I need to handle unnecessary?
source to share
If the buttons are dynamic you must use document.on.
I figured your fiddle would work like this: http://jsfiddle.net/1ehy66k9/
$(document).on('click', '.ui-btn', function() {
alert("Clicked with Id "+$(this).attr('id'));
});
source to share
you need to send the button to the function as a variable this
:
<a class="ui-btn ui-btn-inline ui-mini" id="check" onclick="chkAlert(this)">click me</a>
and then get it inside the function (you can call it whatever you want inside the function)
var chkAlert = function(caller){
alert("Clicked with Id "+caller.id);
};
source to share