348

In jQuery, if I have a reference to an element, how can I determine what kind of element it is, for example, an input or an dropdown? Is there any way to find out?

Duplicate:

How can I determine the element type of a matched element in jQuery?

3
  • 5
    Duplicate: stackoverflow.com/questions/341900/… Mar 3, 2009 at 22:06
  • 10
    May be a duplicate, but it has a better answer, and the other one is tagged badly. Sep 20, 2012 at 21:41
  • 2
    @Rumpleteaser If "the other one" is tagged badly, then why don't you make use of edit button below it and tag it perfectly?
    – trejder
    Apr 16, 2015 at 7:45

3 Answers 3

617

The following will return true if the element is an input:

$("#elementId").is("input") 

or you can use the following to get the name of the tag:

$("#elementId").get(0).tagName
5
  • 59
    When using $("#elementId").get(0).tagName the tagName will be in all caps - TR, DIV etc etc
    – Jarede
    Oct 18, 2012 at 14:41
  • 7
    okay, so the tagName property returns an all caps value, I just did a .toLowerCase() on it Aug 15, 2013 at 12:15
  • 9
    var elementType = $(this).prop('tagName'); May 9, 2014 at 0:58
  • 5
    Using nodeName might be more consistent across different browsers: stackoverflow.com/questions/4878484/… Jun 2, 2014 at 18:12
  • 2
    Note, that using $("#elementId").is(":input") will generally tell you, if you're dealing with any kind of form element, without checking for its specific type (reference).
    – trejder
    Apr 16, 2015 at 8:11
14

You can use .prop() with tagName as the name of the property that you want to get:

$("#elementId").prop('tagName'); 
4

It is worth noting that @Marius's second answer could be used as pure Javascript solution.

document.getElementById('elementId').tagName

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.