Get elements 'tag name' by searching Class name

I'm trying to get element tag name which is associated with the element with a certain class name.

I know I can do these two lines of code to get the class name and the tag name.

document.getElementsByTagName("regeneratePostnatal"); document.getElementsByClassName(returnedPatientID);

I'm just asking if there is a way of doing something like below to get the tag name which belongs to the element with the class name?



var element = document.getElementsByClassName("oneID")[0].tagName; document.getElementById("returnedValue").innerHTML = element; var element2 = document.getElementsByClassName("oneID")[0].nodeName; document.getElementById("returned2Value").innerHTML = element2;

<a class="oneID" name='regeneratePostnatal'>Click</a> <div id="returnedValue"></div> <div id="returned2Value"></div>

I'm trying to get the name attribute value so that it'll show "regeneratePostnatal"

-------------Problems Reply------------

Are you trying to get the attribute name value?

var element = document.getElementsByClassName("oneID")[0].getAttribute('name');
document.getElementById("returnedValue").innerHTML = element;

<a class="oneID" name='regeneratePostnatal'>Click</a>
<div id="returnedValue"></div>

The property you are looking for is nodeName:



Yes you can get the tagName but you need to use index as getElementsByClassName returns a collection of array. So to get the tagName:


//Or in a loop
var len = document.getElementsByClassName('YourClass').length;
for(var i=0;i<len;i++){

Category:javascript Views:1 Time:2018-10-09

Related post

Copyright (C), All Rights Reserved.

processed in 0.182 (s). 11 q(s)