[Javascript] Something better than getElementById
Paul Novitski
paul at novitskisoftware.com
Sat Oct 22 01:53:58 CDT 2005
At 05:08 PM 10/21/2005, Terry Riegel wrote:
>The only reason I need to getElementById is to set the background
>color to indicate the link is selected. Is there a way to set an
>attribute without looking up the id? I am looking for ways to speed
>up my pages, some have hundred's of ID's.
Terry,
You can pass references to the objects themselves instead of their ids:
<tag onclick="doSomething(this);">
...
function doSomething(argObject)
{
argObject.className = "selected";
}
If you prefer to avoid inline scripting, as I do, you can accomplish
the same thing this way:
function Initialize()
{
...
oObject.onclick = doSomething;
}
function doSomething(evt)
{
// cancel event-bubbling
if (evt) { event = evt; }
event.cancelBubble = true;
// change class
this.className = "selected";
}
Peter-Paul Koch has some good tutorials on event processing in
javascript at http://www.quirksmode.org/
Regards,
Paul
More information about the Javascript
mailing list