DOM help. setAttribute won't work

F

fj

I have no problem nevigate and neil down the element I want to manipulate.

What I want to do is set the css class of an item according to
window.location (URL). But it doesn't work.

I call this function in
<body onload="SelectMenuItem();" >

here is the function
function SelectMenuItem() {
....
//i is a variable I set before.
var nodes_LI = document.getElementById("TopNav").childNodes;
for (j=0; j<nodes_LI.length; j++) {
nodes_LI[j].firstChild.removeAttribute("class");
}
nodes_LI.firstChild.setAttribute("class", "highlighted");
....
}
 
D

DU

fj said:
I have no problem nevigate and neil down the element I want to manipulate.

What I want to do is set the css class of an item according to
window.location (URL). But it doesn't work.

I call this function in
<body onload="SelectMenuItem();" >

here is the function
function SelectMenuItem() {
...
//i is a variable I set before.
var nodes_LI = document.getElementById("TopNav").childNodes;
for (j=0; j<nodes_LI.length; j++) {
nodes_LI[j].firstChild.removeAttribute("class");

It seems to me that you're trying to remove the attribute class
specification. You are not trying to remove the attribute node value.
}
nodes_LI.firstChild.setAttribute("class", "highlighted");


Why not then just use DOM 2 HTML method to do this?

if(nodes_LI.firstChild.nodeType == 1)
{
nodes_LI.firstChild.className ="highlighted";
//http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-95362176
}
else
{
// code here
};

It's kinda difficult to offer you alternatives or guess better ones
since you have not provided any url nor a complete description of what
you were trying to do exactly.

DU
 
F

fj

DU said:
fj said:
I have no problem nevigate and neil down the element I want to manipulate.

What I want to do is set the css class of an item according to
window.location (URL). But it doesn't work.

I call this function in
<body onload="SelectMenuItem();" >

here is the function
function SelectMenuItem() {
...
//i is a variable I set before.
var nodes_LI = document.getElementById("TopNav").childNodes;
for (j=0; j<nodes_LI.length; j++) {
nodes_LI[j].firstChild.removeAttribute("class");

It seems to me that you're trying to remove the attribute class
specification. You are not trying to remove the attribute node value.
}
nodes_LI.firstChild.setAttribute("class", "highlighted");


Why not then just use DOM 2 HTML method to do this?

if(nodes_LI.firstChild.nodeType == 1)
{
nodes_LI.firstChild.className ="highlighted";
//http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-95362176
}
else
{
// code here
};

It's kinda difficult to offer you alternatives or guess better ones
since you have not provided any url nor a complete description of what
you were trying to do exactly.

DU


Thank you. It's works fine now.

I was trying to set the current menu high-lighted by change it's css
class.Now I know I should use .className instead of .attribute.

Doesn't class="myCSSClass" look like an attribute of an element?

-fj
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top