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. (
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
Post a Comment