Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Tag from id => javascript/jquery

I am curious if it is possible to get tag equivalent to jquery's $('tag_name') by just having an id at hand? For example:


    var some_id = document.getElementById('my_Id');
    //and then do something like
    var get_tag = some_id.its_tag;

Is something like this possible?

I know we can get all tags into one place, like list, and then loop through that list and check each id, but curious if there is something that can be done elegantly?

Thanks!

Alternative is known, but would be great if original question actually has some answer too.


    var some_id = document.getElementById('my_Id');
    //and then do something like
    var get_tag = some_id.its_tag;

like image 818
LRS Avatar asked Nov 23 '25 13:11

LRS


2 Answers

var some_id = document.getElementById('my_Id');
var tag = some_id.tagName;
like image 55
php guy Avatar answered Nov 26 '25 02:11

php guy


You can use the Element.tagName property (it represents the tag name in uppercase):

var some_element = document.getElementById('my_Id');

console.log(some_element.tagName); // => P
<p id="my_Id">Hello</p>
like image 36
MrGeek Avatar answered Nov 26 '25 02:11

MrGeek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!