Grabeorama Posted December 19, 2010 Share Posted December 19, 2010 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 More sharing options...
Ingolme Posted December 19, 2010 Share Posted December 19, 2010 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 More sharing options...
Grabeorama Posted December 19, 2010 Author Share Posted December 19, 2010 I'm using the function:function toggle() { this.className = "show"; }and using it as <li onclick="toggle();">, but when I click it, nothing happens...What do you mean by events? Link to comment Share on other sites More sharing options...
jeffman Posted December 19, 2010 Share Posted December 19, 2010 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 More sharing options...
Grabeorama Posted December 19, 2010 Author Share Posted December 19, 2010 Thanks Deirdre's Dad, that fixed the problem. I had this problem before, I keep forgetting about scope. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.