Detect element tag name by its class

I would like to know if there is a way to get the name of a element by its class or id.

for exemple <input class="some-class" type="text" /> returns "input"

Thanks.

Answers:

Answer

Try tagName :

var input = document.getElementsByClassName("some-class")[0];
alert(input.tagName);

JSFiddle: http://jsfiddle.net/PG656/1/

Answer

you can use:

var input = document.getElementsByClassName("some-class")[0]
alert(input.type);
alert(input.name);
Answer

use this:

$('.classTag').get(0).tagName;

or this:

$('.classTag')[0].tagName;

for ex we have this markup:

<div class=classTag>my div </div>


var $tag = $('.classTag')[0].tagName; //this will return 'DIV'as result
alert($tag);
Answer
var tagName = $('.some-class').prop('tagName');

the name of the tag will be returned in capital letters

Answer

Actually $('.some-class') returns an array of elements with this class, so you have to loop through it:

$('.some-class').each(function(){ 
    console.log(this.nodeName); 
});

this will output their node names to console

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.