Forum Moderators: open

Message Too Old, No Replies

changing the class of a parent object from which a link resides

         

miker

7:37 am on Jun 4, 2009 (gmt 0)

10+ Year Member



Hi guys,
i need to change the class of the <li> element when the child href is the current location, i.e when on page 1 the <li> needs to be class='abc'

any help?

if it makes it any easier to do, i currently have a script set up that applies a class to the <a> tag which is below. if there is a way to edit this script so that the class is applied to the parent <li> tag could someone show me how?

thanks

<ul id="v_menu" class="vlink">
<li>
<a href="page1.htm">Advisory Notes</a>
<li>
<a href="page2.htm">Agreements And Working Arrangements</a>


function selfLink(oRootElement,oClass,oExpand,oLink) {
var tmpLink;
if(!document.getElementsByTagName¦¦!document.childNodes) { return; }
oRootElement = document.getElementById(oRootElement);
if( oLink ) {
tmpLink = document.createElement('a');
tmpLink.setAttribute('href',oLink);
}
for( var x = 0, y = oRootElement.getElementsByTagName('a'); y[x]; x++ ) {
if( y[x].getAttribute('href') && !y[x].href.match(/#$/) && getRealAddress(y[x]) == getRealAddress(oLink?tmpLink:location) ) {
y[x].className = (y[x].className?(y[x].className+' '):'') + oClass;
if( oExpand ) {
oExpand = false;
for( var oEl = y[x].parentNode, ulStr = ''; oEl != oRootElement && oEl != document.body; oEl = oEl.parentNode ) {
if( oEl.tagName && oEl.tagName == oRootElement.tagName ) { ulStr = oEl.MWJuniqueID + (ulStr?(':'+ulStr):''); } }
stateToFromStr(oRootElement.id,ulStr);
} } } }

daveVk

8:10 am on Jun 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



try change

y[x].className = (y[x].className?(y[x].className+' '):'') + oClass;

to

var z=y[x].parentNode;
z.className = (z.className?(z.className+' '):'') + oClass;

miker

8:21 am on Jun 4, 2009 (gmt 0)

10+ Year Member



thank you, works perfect now :)