0

I have a javascript method -testElement() which is called on onclick event -

<script type = "text/javascript">

    function testElementType(element){

        var elemId = $(element).attr("id");
        //Can I determine the 'element' from the 'elemId' ?

        // if element is div - do something
        // if element is button - do another thing

    }

</sccript>

Now consider the following code -

<div onclick="testElementType(this);" id="testDiv" > Test Div </div>
... 
<button onclick="testElementType(this);" id="testBtn" > Test Button </button> 

So when either of 'testDiv' or 'testBtn' is clicked then the 'testElementType() method is called. In this method I can get the element Id by jquery - $(element).attr("id"); and store it in 'elemId'

Is there any way to find the the type (ie - whether it is a div or button ) of element from 'elemId'?

I know in practice most of the situation we don't have to bother about the element type. Because we can call to different function for two type of element click if we want to do two different type of task.

Razib
  • 10,965
  • 11
  • 53
  • 80
  • You have a web page. Give them ids and then forget their types. Weird – Ed Heal Feb 21 '15 at 15:37
  • 2
    have you tried to access the `.tagName` or `.nodeName` attribute of the element? – Halfpint Feb 21 '15 at 15:38
  • bit like looking at a car's license plate to go look up it's color, when you can look right at color of car itself – charlietfl Feb 21 '15 at 15:40
  • find duplicate question http://stackoverflow.com/questions/608410/finding-the-type-of-an-element-using-jquery – Pushker Yadav Feb 21 '15 at 15:42
  • @Ed Heal why did you think I'm forgetting the types. Stackoverflow is a knowledge sharing site. People can exchange their knowledge here. My questions was intent to knowing - whether it is possible from the id to determine the element type. My last portion of the question show a proper indication that I know about the other way. Please don't get it wrong. – Razib Feb 21 '15 at 16:06
  • Perhaps the best solution is not to forget them in the first place – Ed Heal Feb 21 '15 at 16:14

5 Answers5

3

You dont need to use the id value here. You pass the object into the function iself so all you need to do is this:

function testElementType(element){
    var tag = element.tagName;
    if(tag=='DIV') // its a div
    if(tag=='BUTTON') // its a button
}
Jackson
  • 3,476
  • 1
  • 19
  • 29
1

using jquery $("#elementId").is("button") will return if elementId is button.

for more answer here

Community
  • 1
  • 1
Pushker Yadav
  • 866
  • 1
  • 8
  • 15
1

Taken from docs, the tagName selector will return the elements type, you can use it like this.

function testElem(e) {
    var t = e.tagName;

    // Print the tag name
    console.log(t);
}
Halfpint
  • 3,967
  • 9
  • 50
  • 92
1

Something like...

function testElementType(element){
    var id = $(element).attr('id'),
        tag = $(element).prop("tagName");
});

See this demo fiddle

Ted
  • 14,757
  • 2
  • 41
  • 58
0

Try this way to get element type by id

$("#elementId").get(0).tagName
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103