0

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);
}
Paresh3489227
  • 845
  • 11
  • 22
  • can you share the related html also – Arun P Johny May 21 '14 at 05:28
  • i edited for more clearance. not able to share html because i am working on live. this code is fetching jquery dynatree parent nodes and changing the folder icon by appending className – Paresh3489227 May 21 '14 at 05:38
  • i edited static code which works fine. tested i only wanted to do in a dynamic way. every current node i want to add parentNode.parentNode.firstChild.className+='parent1' – Paresh3489227 May 21 '14 at 05:40
  • Html like
    now can you help me @Arun P Johny
    – Paresh3489227 May 21 '14 at 06:28
  • Maybe you should read the jquery docs first, as your usage of jquery is not quite correct. – rhgb May 21 '14 at 06:53
  • I go through the examples on w3school. .find() is perfect for me in this situation but didn't working. i debugged also. In that examples there are html tags in that you want to find. here i want all [object HTMLSpanElement] from loop. – Paresh3489227 May 21 '14 at 07:10

1 Answers1

0

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.

Paresh3489227
  • 845
  • 11
  • 22