0

I have hold of an "A" tag, and I would like to know what classes it's parents have got...so:

<div class="topone">
    <div class="middleone">
        <a href="#" class="thisone">tag</a>
    </div>
</div>

So I've got "thisone", but I want to know if there is a div above it somewhere that has the class of "topone"

...no jquery though, can't use jquery for this

2

4 Answers 4

4

You can use the .className property to access an element's class and .parentNode to go to the parent element.

var classes = [];
for(var el = document.getElementById('#yourlink'); el; el = el.parentNode) {
    classes.push(el.className);
}

Note that classes[0] contains the classes of the element itself - but I'm sure it's no problem for you to modify the code accordingly if you do not want that.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use .parentNode and className properties.

This is the way:

document.getElementById("MyElement").className

Fiddler Demo

1 Comment

But where has "MyElement" come from? This doesn't really answer the question
0
var element =document.getElementById("topone");      
var liArray = element.childNodes;
      var i=0, item;
    if(element != null)
    {var status=0;
      while (item = liArray[i++]) {
        if (item.nodeType == 1) {
         if(item.className=="thisone"){status=1;}
        }
      }
    if(status==1){alert('it have parent node');}else{alert('it doen`t have parent node');}
    }else{alert('it doesn`t have parent node');}

i have tried at my level as per your requirement may it help you

Comments

0

You can write something like this

give your a tag an id, say bottomone

var e = document.getElementById("bottomone").parentNode.className will give you the class name as middle one.

and for the upper div, you can write

var e = document.getElementById("bottomone").parentNode.parentNode.className

1 Comment

but he is not sure where the element is

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.