Javascript nextSibling keep both objects, current and next. Is it possible?

207 views Asked by At

I am working with a table and i need to work with current and the next row. My function looks like this:

function test(row) {
    alert(row.id); //it gives me the current row id, it works.
    nextrow = row.nextSibling.nextSibling; //not sure why, but i have to use it twice to get the right one.
    alert(nextrow.id); //this gives me the next row's id, also works.
    alert(row.id); //this also gives me the next row's id...it should give me the current one.
}

Any ideea if i can store the actual object somewhere so it won't change? Thank you.

1

There are 1 answers

0
Samuel Rossille On

HTML nextSibling property returns the next sibling node including text, line breaks and comments.

That's probably why you have to call it twice.

Concerning the fact that row.id changes, I recommend rechecking the code. If there is no affectation of row or row.id, there is no way row.id changes between line 1 and 4 of the code. Is that your real code, or arranged code for increased readeability ?

For DOM manupulation, i highly recommend to use jQuery if you can. It's a very simple and powerful library which makes DOM manipulation very easy.

If you can't use it in your context, you shoud write a utility method called nextRow that will take a TR as an argument and return the next TR. It will use nextSibling property until it finds a node which tagname is actually TR.