
Originally Posted by
Veronica
<a href="#" onClick="style.backgroundColor='#84DFC1';">link to change color</a>
One should not count on the style object, or indeed any element properties being accessible unqualified from event listeners. Always use the this operator.
The only identifiers one should expect to find is event (where the W3C event model is implemented) and any global.

Originally Posted by
shyne
This works BUT when I click on another link then both links stay in same bgcolor, I mean if I have more than one link then when you click on any other link it should change the previously clicked div link's bgcolor back to normal.
any help????
Keep a record of what element (if any) is active:
Code:
var highlightLink = function () {
var active = null, colour = '#84DFC1';
if (this.attachEvent) this.attachEvent('onunload', function () {
active = null;
});
return function (element) {
if ((active != element) && element.style) {
if (active) active.style.backgroundColor = '';
element.style.backgroundColor = colour;
active = element;
}
};
}();
Untested. Change colour variable value to the colour of your choice.
In place of the assignment suggested by Veronica, call the highlightLink function defined above. So, rather than
Code:
onclick="style.backgroundColor = '#84DFC1';"
use:
Code:
onclick="highlightLink(this);"
If you wanted to use different colours for different links, that's also possible, though a slight modification would be necessary.
Hope that helps
Bookmarks