...

View Full Version : event.srcElement



mark87
01-27-2006, 08:54 PM
Can anyone explain why this just alerts [object]. I'm trying to change the background of a tr when it's clicked on, I only added in the alerts to see why it wasn't working.

<tr class="a" onclick="sel()">

function sel() {
var x = event.srcElement;
alert(x);
}

Is there a better method?

Basscyst
01-27-2006, 09:18 PM
I usually just pass the object with the call of the function:



function sel(obj)
{
alert(obj);
}
____________

<input type="button" onclick="sel(this);" />


Whether or not that's better, I couldn't say.

As far as why it's alerting an object, is simple. The element "tr" is an object, it being the source element of the event. If you wanted to know the type of object or the object tag name, you could do something like this:



function sel(obj)
{
alert(obj.tagName);
alert(obj.getAttribute('type'));
}


The object reference you are left with is the same as saying:



function go()
{
var obj=document.getElementById('objID');
alert(obj);
}



Oh, and to change the background-color:


function go(obj)
{
obj.style.backgroundColor='#336699';
}

Also note, if you are changing the background color of a tr element, and the td element has a background color too, the td element's color will show up over the tr elements color.

Basscyst



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum