JS - make nodeValue the id - but how...

bonzo meier

Member
Messages
47
Reaction score
0
Points
6
hello.

i have a menu set up like this:

<div id="menu">
<a class="active">abc</a>
<a class="passive">cde</a>
</div>

this is part of a typo3-template. whatever, what i need to do for design reasons is to replace the links (the 'abc', 'cde') with whitespace and set individual id´s to them, which means e.g. having

<a class="active" id="abc"></a>

in the end. what i tried is this:

function pimp_navi(id)
{
with (document.getElementById(id)){
var links = getElementsByTagName('a');
for(i=0;i<links.length;i++)
{
var name = links.firstChild.nodeValue;
links.id = name;
links.firstChild.nodeValue = "";
}
}
}

it is removing the link wording (nodeValues), but the id is not assigned. ideas, anyone?

peace, bonzo
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
Well, change it to not use that with() statement first of all. And second, it works fine for me(without the with() that is). After I run it, I'm able to get the links by their id without any error.
 

bonzo meier

Member
Messages
47
Reaction score
0
Points
6
yes, but removing the with-statement would probably lead to all the links on the page being handled by the script. i just want the links in the <div id="menu"> to be changed.

thanks and peace,
bonzo
 

devongovett

New Member
Messages
33
Reaction score
0
Points
0
You could also try:

Code:
function pimp_navi(id)
{
   with (document.getElementById(id))
  {
     var links = getElementsByTagName('a');
     for(i=0;i<links.length;i++)
    {
       var name = links[i][B].innerHTM[/B]L;
       links[i].id = name;
       links[i].[B]innerHTML[/B] = ""; 
    }
  }
}
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
I wasn't saying to just remove the with() statement, I was saying to modify the code to not use it. For example,

Code:
function pimp_navi(id)
{
    var links = document.getElementById(id).getElementsByTagName('a');
    for(var i=0;i<links.length;i++)
    {
        var name = links[i].firstChild.nodeValue;
        links[i].id = name;
        links[i].firstChild.nodeValue = "";
    }
}

Using with() is generally not a good idea.
 
Last edited:

bonzo meier

Member
Messages
47
Reaction score
0
Points
6
hi,
woiwky, thank you very much! this is working perfectly - sorry i got you wrong in the first place...

two more things:
- would you like to explain why one shouldn´t use the with()-statement (apart from that it didn´t work in this case?)?
- would someone from x10 PLEEZE start a tutorial on how to close threads? as i am writing this reply i don´t see any possibility of having this thread closed - neither do i get any closing options in the thread tools menu or elsewhere...

peace,
bonzo
 

bonzo meier

Member
Messages
47
Reaction score
0
Points
6
hi,
turns out good that i couldn´t close the thread:

why doesn´t it work on ie6? ideas?

Code:
function pimp_navi(id)
{
		var links = document.getElementById(id).getElementsByTagName('a');
    for(var i=0;i<links.length;i++)
    {
	var class = links[i].className;
	var exclude = "navi_second";
	if(class != exclude){
        var name = links[i].firstChild.nodeValue;
        links[i].id = name;
        links[i].firstChild.nodeValue = "";
		}
    }
}

thanks in advance,
peace,
bonzo
 

sybregunne

Member
Messages
54
Reaction score
0
Points
6
sometimes ie6 will add a blank childenode.... check if you are using IE6 using the browser.useragent or something... (I forgot)


Code:
function pimp_navi(id)
{
    var links = document.getElementById(id).getElementsByTagName('a');
    for(var i=0;i<links.length;i++)
    {
	var class = links[i].className;
	var exclude = "navi_second";
	if(class != exclude){
          temp=links[i].firstchild;
          while (temp.nodeType!=1) {
            temp=temp.nextSibling;
          }
          var name = temp.nodeValue;
          links[i].id = name;
          temp.nodeValue = "";
        }
    }
}
[code]

I don't know if this will work... but try it anyway...
 

bonzo meier

Member
Messages
47
Reaction score
0
Points
6
no, sorry, this is not working.

even if i change the line
Code:
          temp=links[i].firstchild;
to
Code:
          var temp=links[i].firstchild;
...which i think is necessary, it doesn´t work on any browser. however, the nextSibling-approach looks promising to me, any ideas how i can get on with this?

thank you all,
peace,

bonzo
 
Top