I need to get the ID of the element when the TextBox gets focus and print it to the console . How can I do this? , I wrote some code but it always prints txt00
I have this in the HTML:
<asp:TextBox ID="txt00" class="cTxt" runat="server"></asp:TextBox>
<asp:TextBox ID="txt01" class="cTxt" runat="server></asp:TextBox>
<asp:TextBox ID="txt02" class="cTxt" runat="server"></asp:TextBox>
JQuery code:
$('.cTxt').focus(function () {
var id = $('.cTxt').attr('ID');
console.log(id);
});
You don't need jQuery inside the function, instead use the context object
this
:The callback you send to focus, like any handler for a DOM element that receives an event, is passed an Event object as an argument, that object holds a reference to the element that just fired the event. and so you could get the
id
instead of using Jquery's select function again.Using an example class that I invented, this would be the solution:
UPDATE:
Using an example like the one you have: