i have many input fields like this
<input type="radio" name="name" id="name" onchange="enableTxt()" />
when i click this radio button i wanna capture the id of the radio input. i am using the following code
function enableTxt() { var id = $(this).attr("id"); alert(id); }
Am getting this error
a.attributes is undefined
We can get the ID of the element that's clicked from the click event handler. We can either get the value from this if we use a regular function. Or we can get the same value from the event object of the click event handler.
The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.
Answer: Use the jQuery attr() Method You can simply use the jQuery attr() method to get or set the ID attribute value of an element. The following example will display the ID of the DIV element in an alert box on button click.
JQuery val() method: This method return/set the value attribute of selected elements. If we use this method to return value, it will return the value of the FIRST selected element. If we use this method to set value, it will set one or more than one value attribute for set of selected elements.
HTML:
<input type="radio" name="name" id="name" onchange="enableTxt(this)" />
JS:
function enableTxt(elem) { var id = $(elem).attr("id"); alert(id); }
The html
<button class="destroy" id="123">
The jQuery
$('button.destroy').on('click', function(e){ e.preventDefault(); console.log(this.id);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With