I have two nodes with elements get by tag name.
Now i want these two nodes merged in one node. How to do it ?
ie:
list1 = document.getElementById("menu1").getElementsByTagName("a");
list2 = document.getElementById("menu2").getElementsByTagName("a");
I assume you want to add the nodes in list2 to the nodes in list 1
Code:
var list1 = document.getElementById("menu1").getElementsByTagName("a");
var list2 = document.getElementById("menu2").getElementsByTagName("a");
for(i=0; i < list2.length; i++){
list1.push(list2[i]);
}
I assume you want to add the nodes in list2 to the nodes in list 1
Code:
var list1 = document.getElementById("menu1").getElementsByTagName("a");
var list2 = document.getElementById("menu2").getElementsByTagName("a");
for(i=0; i < list2.length; i++){
list1.push(list2[i]);
}
No. A DOM collection of elements is a referential collection, not an Array, thus you can not use upon them some of the Arrays methods, including push(). See also mrhoo's post.
var list1 = document.getElementById("menu1").getElementsByTagName("a");
var list2 = document.getElementById("menu2").getElementsByTagName("a");
var list12=[];
for(var i=0;i<list1.length;i++){list12[list12.length]=list1[i]}
for(i=0;i<list2.length;i++){list12[list12.length]=list2[i]}
// from now one use list12 array
var list1 = document.getElementById("menu1").getElementsByTagName("a");
var list2 = document.getElementById("menu2").getElementsByTagName("a");
var list12=[];
for(var i=0;i<list1.length;i++){list12[list12.length]=list1[i]}
for(i=0;i<list2.length;i++){list12[list12.length]=list2[i]}
// from now one use list12 array
I still dont know what is going on, especialy that with only one geElementByID line it works fine...
First: you should have not opened another thread with the same problem. It is confusing for everybody. It is called cross-post, and it should be avoided. Now I must find a way to merge logically both threads, in order to create a single one. Don't think that posting 2 versions of a single problem will multiply the chances to find a solution. On the contrary, you will confuse everybody.
Or I might decide to delete that thread as redundant.
Second: I wait a code of you, in which you have tried to implement our last advices. Post the code or a link to a test page, to see the up-to-date changes.
You are not combining two nodelists into one node list.
The two nodelists are not changed.
List1 is still list1, list2 is still list2.
You are building up a third object, an array that contains the elements from the two nodelists.
Use the array, not the nodelist, to pass your function the elements contained in both nodelists at one time.
Thats why i opened a second thread. In the first nobody was able to help me since the code i have to "upgrade" is using nodes not arrays and i may not change that.
And with an array it was not working at all.
Originally Posted by Kor
First: you should have not opened another thread with the same problem. (...) Or I might decide to delete that thread as redundant.
So delete it IF they are redundant.
The first thread is about merging arrays. Then i realized the objects I was trying to merge are not arrays but nodes and merging them into an array is not resolving the problem. I wrongly described the problem and because of it I thought nobody was able to help me.
The second one (this one) is about merging nodes. And it seems (to me) to be the case.
Originally Posted by Kor
Second: I wait a code of you, in which you have tried to implement our last advices. Post the code or a link to a test page, to see the up-to-date changes.
Do you really want me to post the code... ?
It was just a copy/paste work i dont think anything should be wrong there... but well, ok. If it help.
Your Object supposes to have as arguments the id's of the links. But starting with "m5, m6,m7, m8", there will be no correspondent id, as your HTML elements have other ids : "m17,m18,m19,m20". This is the error, not the Array. The array is ok:
Code:
function initPage() {
var list1 = document.getElementById("phrase-menu1").getElementsByTagName("a");
var list2 = document.getElementById("phrase-menu2").getElementsByTagName("a");
var list=[];
for(var i=0;i<list1.length;i++){list[list.length]=list1[i]}
for(i=0;i<list2.length;i++){list[list.length]=list2[i]}
for(i=0;i<list.length;i++){
alert(list[i].id+' | '+ list[i].firstChild.nodeValue)
}
}
window.onload = initPage
>IE don't like that:
>m8: "Text8", // remove the last comma
I dont care Interpreter is Opera-based only.
But for the purity of the code, i will remove it and remeber in the future.
And the mistake You have found is no problem at all - it is only my mistake here in the forum. When i paste the code here i have cut elements of both phrase-menu`s to only 4. It is useless to put here both 16 of them, making 32 in all. and Thats why the phrase-menu2 elements start at m17 not at m5. My simple mistake here in the forum only.
Bookmarks