...

View Full Version : attachEvent change style



Eben
07-24-2009, 08:01 PM
that suppose to bind onclick and change the backgroung color,
this.style.backgroundColor doesnt work with IE, any solution?


<script type="text/javascript">
var els = document.getElementsByTagName('input');
for (i = 0; i < els.length; i++) {
var el = els[i];
if (el.addEventListener) {
el.addEventListener ("click",changeBG,false);
} else if (el.attachEvent) {
el.attachEvent ("onclick",changeBG);
}
}

function changeBG() {
this.style.backgroundColor = 'red';
}
</script>

Amphiluke
07-24-2009, 08:16 PM
Try this:

function changeBG(evnt) {
var ev = (evnt) ? evnt : event;
if (ev.srcElement) {
ev.srcElement.style.backgroundColor = 'red';
} else {
ev.target.style.backgroundColor = 'red';
}
}

Eben
07-24-2009, 08:46 PM
thx a lot

Eben
07-24-2009, 09:30 PM
hey, one more question, how do i use setInterval with this, i need the background to disappear after some time. i cant get it working.

ckeyrouz
07-24-2009, 10:03 PM
change your function like this:


function changeBG(evnt)
{
changeBGColor(evnt,'red');
window.setTimeout("changeBGColor("+evnt+",'white')",2000); //I added the comma between the two parameters inside the function call
// the system will wait 2000 milli seconds before calling back the //method and setting the background color back to white
}

function changeBGColor(evnt,colorName)
{
var ev = (evnt) ? evnt : event;
if (ev.srcElement)
{
ev.srcElement.style.backgroundColor = colorName;
}
else
{
ev.target.style.backgroundColor = colorName;
}
}

Eben
07-24-2009, 10:49 PM
thank you for your effort and time, i get this error:

missing ] after element list

?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum