How to get the id of an anchor tag in jQuery? -


How to get an ID of an anchor tag in jQuery? This is the tag.

  & lt; Ul class = "formfield" & gt; & Lt; Li class = "selected" & gt; & Lt; A href = "" id = "text" & gt; Text & lt; / A & gt; & Lt; / Li & gt; & Lt; Li & gt; & Lt; A href = "" id = "textarea" & gt; Teddera & lt; / A & gt; & Lt; / Li & gt; & Lt; / Ul & gt;  

I need to get an id, i.e., text, text etc. in a variable.

I tried to do something like that, but the field value value. (

a)

id of a field To get the attribute, you have:

  $ ('ul.formfield a'). Click (function () {var id = $ (this) .attr ('id'); alert (id);});  

To get text content of a tag (the text between opening and closing tags), you will do this:

 < Code> $ ('ul.formfield a'). Click (function () {var text = $ (this) .text (); Alert (text);});  

Please pay attention to the use of $ (this) inside the click function. You were re-using the selector which will not do what you want to do. Inside the event handler, this refers to that element, so with the code above you will get 'text' or 'textaria' which you clicked on.


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -