Search code examples
javascriptjqueryjquery-dynatree

filtering object HTMLSpanElement and add className to that object


This code alerting 1. object HTMLLIElement 2. object HTMLSpanElement.

I only want to filter all object HTMLSpanElement and append className+='parent1'

var htmlLabelElementObj = HtmlDocObj.getElementById(CurrentNodeId);
var current = htmlLabelElementObj.parentElement.parentElement.parentNode.parentNode.parentNode;
while (current.parentNode){
     current = current.parentNode.parentNode.firstChild;
     alert(current);
}

This below code is static. I am doing like this in a dynamic way.

htmlLabelElementObj.parentElement.parentElement.parentNode.parentNode.parentNode.firstChild.className+=' parent1';
htmlLabelElementObj.parentElement.parentElement.parentNode.parentNode.parentNode.parentNode.parentNode.firstChild.className+=' parent1';
htmlLabelElementObj.parentElement.parentElement.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.firstChild.className+=' parent1';

This above code is setting the className by adding to current node. parentNode.parentNode.firstChild every node.

EDIT: i tried find and filter but it not working.

if($('current').find('[object HTMLSpanElement]'))
{
    alert(current);
}

if($('current').filter('[object HTMLSpanElement]'))
{
    alert(current);
}

Solution

  • Here is solution to my own question. dynamic getting all node with span filteration

    var testspan='';
    var current = htmlLabelElementObj.parentNode.parentNode.parentNode;
       while (current.parentNode){
     //this current variable add every time two parentNode to reach li of parent
            current = current.parentNode.parentNode; 
            if(current != null)
            {
                 // this code to reach span of li and change the span icon of parent class
                testspan = current.firstChild;
                if(testspan == '[object HTMLSpanElement]' && testspan.className != ' parent1')
                testspan= testspan.className+=' parent1';   
            }
            else
            {
                break; // this is for preventing an exception of childnode
            }
        }
    

    Hope this will help someone. like i suffered for making static code to dynamic.