I have a page that has two elements. One is navigation, and the other is an iframe where the links in that navigation change the pages. It works fine except I need the background color of the nav item to stay highlighted after its been clicked (and the corresponding page is therefore being viewed). In other words, I just need some simple javascript to say, "on click, change the bground of this link to X and make the others go back to normal". Make sense? Probably not hard, I just don't know javascript.
thanks
Last edited by dtrachtman; 09-23-2012 at 03:32 PM.
I'm pretty sure this is a CSS question. However, in JavaScript I think you would need to dynamically change the class of the element in question to achieve this. You can use the window.location.hash object to let JavaScript know which page you're on, and change the class as necessary. I've never actually done this myself, but if I were to try, I'd do something along the lines of:
Thanks for the advice. I dont think this would be CSS related because its an if/then type of situation. I think I made this more complicated than necessary by even mentioning the iframe.
What about a list of links. When one is clicked, make its bground white and turn off all the other link backgrounds. I think that would be all I need.
I'm sorry, I didn't read your first post carefully enough. What you're saying is that you have a single page with navigation, and that navigation is affecting the contents of the iframe? In that case, it's easy.
All you need to do is create the class in CSS for the "active" link. I'm pretty sure this should work, but I haven't got time to test it right now. Let me know.
Okay, at the risk of being rude it appears you didn't really know what to do with the sample code I provided. I wasn't attempting to give you fully functional code, just a nudge in the right direction. Anyway, I've taken the liberty of writing what I think it is you want, and I've uploaded it to my website so you can download it at your leisure.
The index file is where all the code is written, but I'm not going to paste in the code to webdeveloper.com because the new code presentation sucks. You can view the source of the file I linked you to and copy & paste the code from there.
does 3 completely different things on Chrome 21, FF 15 and IE8...
Oh, sorry. I didn't test it in all browsers for some reason. Anyway, the problem is just that the other browsers don't accept the ID attribute as a valid target, it must be a name attribute instead. The iframe wasn't given a name, but I've updated it now so the example is in working order.
Bookmarks