0

I have got a html page, with a navigation bar. Ideally, once user select one option, that option background image would change. However, I was not able to achieve that, by using javascript to dynamically change the list property.

The html:

<html>
<head>
<link href="includes/css/content.css" rel="stylesheet" type="text/css">
<script>
function select(id)
{
    alert(id);
    var list = document.getElementsByTagName('li');
    for(var i=0;i<list.length;i++)
    {
        list[i].class='random';
    }
    document.getElementById(id).class='selected';
}
</script>
</head>
<body>
<div id="mainContent">
            <div class = 'nav'>
                <ul>
                    <li class='selected' id='home' onClick="select(this.id)"><a href="#" ><span  style='color:gray'>Home</span></a></li>
                    <li id='system' onClick="select(this.id)"><a href="#" ><span  style='color:gray'>Systems</span></a></li>
                    <li id='temp' onClick="select(this.id)" ><a href="#"><span  style='color:gray'>Notification Template</span></a></li>

                </ul>
        </div>
</body>
</html>

And the css file looks like this:

.nav .selected a{
background-size:cover;
background: url(../images/nav.gif) no-repeat top;
color:white;
}

Is there something I am doing wrong here?

2
  • 2
    list[i].class='random'; should be list[i].className += ' random'; Commented Nov 28, 2012 at 18:04
  • Did .className fix your problem, or is it still not cooperating? Commented Nov 28, 2012 at 19:24

2 Answers 2

2

The property class does not exist for HTMLElement. Use className to set the css class.

For a full list of properties - see http://www.w3schools.com/jsref/dom_obj_all.asp

Sign up to request clarification or add additional context in comments.

Comments

1

If you're setting a class that way, you'll want to use .className [ https://developer.mozilla.org/en-US/docs/DOM/element.className ]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.