[thelist] [JS] hilite function for in-page link

Stephen Caudill SCaudill at municode.com
Wed Jul 9 08:47:51 CDT 2003


I hope someone can clear me up on this.  I have a relatively short FAQ page with in-page links (questions) at the top and answers at the bottom.  At higher monitor resolutions, since the page doesn't scroll much, it's difficult to (at a cursory glance) tell which answer has been linked to (even though the question is reiterated above it).  So my thought was to simply change the color of the question once a link had been clicked... this has proven more difficult than I expected.

here's the first version I tried:

function hilite(){
	var strHash = location.hash;
	var lenHash = strHash.length;
	var strID = strHash.substring(1,lenID);
	document.getElementById(strID).style.color = 'blue';
}

and called it like so:

<a href="#example" onclick="hilite()">example</a>

This did not work because the page never reloads (in-page link) and it never gets a new value for location.hash.  So I tried this:

function hilite(IDhref){
	var lenID = IDhref.length
	var strID = IDhref.substring(1,lenID);
	document.getElementById(strID).style.color = 'blue';
}

and called it like so:

<a href="#example" onclick="hilite(this.href)">example</a> (I even tried different event handlers like 'onmousedown' etc.)

I should note that the elements that are being linked to have id's and not named anchors, thus the getElementById method.  The page I am trying to implement this on is:
http://www.mccinnovations.com/support/faq_test.asp

If anyone could take a look and maybe point me in the right direction, I'd greatly appreciate it.


Thanks,
Stephen Caudill
Web Developer
Municipal Code Corporation
www.municode.com
scaudill at municode.com 


More information about the thelist mailing list