Jquery selectors (search label)

I am sure it is simple, but I cannot understand it. I need to be able to pass the id function of the element and know what that element is.

For instance:

<a id="first"></a>
<input id="last" />

If I know that the identifier is "first", how can I get that tag "a"?

+3
source share
4 answers

This should do it:

var tagName = $("#first")[0].tagName;

[0]is synonymous with get(0). You get the first element from the jQuery object and use the DOM property tagName. This is probably more straight forward in vanilla Javascript:

var tagName = document.getElementById("first").tagName;
+3
source

You can use the DOM property tagNameas follows:

document.getElementById('first').tagName

Or using jQuery you will need to do:

$('#first')[0].tagName
+2
source

.nodeName ( , ), :

$("#first").get(0).nodeName
//or the vanilla js way...
document.getElementById("first").nodeName
+2

$( "# " ) ( "" );.

+1

Source: https://habr.com/ru/post/1744073/


All Articles