Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery onClick capture the id of the element

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 
like image 925
Harsha M V Avatar asked Dec 08 '10 06:12

Harsha M V


People also ask

How do I get the ID of clicked element?

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.

How can I get the ID of an element using jQuery?

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.

How do I know which ID is clicked in jQuery?

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.

How do I set the value of a element in jQuery?

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.


2 Answers

HTML:

<input type="radio" name="name" id="name" onchange="enableTxt(this)" />  

JS:

function enableTxt(elem) {     var id = $(elem).attr("id");     alert(id); } 
like image 130
Calvin Avatar answered Oct 12 '22 11:10

Calvin


The html

 <button class="destroy" id="123"> 

The jQuery

  $('button.destroy').on('click', function(e){     e.preventDefault();     console.log(this.id); 
like image 27
Harry Bosh Avatar answered Oct 12 '22 10:10

Harry Bosh