Jump to content

Changing an elements class


Grabeorama

Recommended Posts

I currently have an un-ordered list with an un-ordered list in it:<ul><li class="hide">2010<ul><li>December</li><li>November</li></ul></li><li>2009</li></ul>What I'd like to do is change the class of the <li> tag from "hide" to "show" when clicked. I've tried changing it using "this.className = 'hide'", but that doesn't seem to work. The other method I thought of was by checking the <li> tag for child elements, and when finding a <ul> tag, it would set it like "ulTag.style.display = 'block';", but I am unsure of how to check the child elements. Any help is appreciated

Link to comment
Share on other sites

What happens when you try to access the className property?It's possible that "this" is refering to the window object. If you're using events, you can access the element with e.target/e.srcElement

Link to comment
Share on other sites

When you attach an event handler to an element using code in "quotation martks", the this keyword does not get rescoped. Instead of referring to the element it refers to the window. The simplest fix would be:function toggle(el) {el.className = "show";}<li onclick="toggle(this);">

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...