PDA

View Full Version : srcElement problem in firefox



reazrana
09-17-2007, 05:30 AM
function clickOnElement(srt)
{
if (srt==0)
{
elm_id = window.event.srcElement.id;
}
}



i want to call this function in onClick where srt=0. it works fine in IE but nor in firefox.

any solution???

jscheuer1
09-17-2007, 05:51 AM
It is unclear what your function does. However, I can tell you that the rough equivalent of MS's event.srcElement in FF and other modern browsers that follow standards in javascript is:


event.target

However, it's more complicated because events aren't handled the same way. In IE, you have the window.event - in all other modern browsers the event is passed automatically (intrinsically) as a part of the execution of the function and/or event - in most cases. In the other cases, the event can be passed to the function.

A typical example of intrinsic event passing with an IE workaround is:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function whatever(e){
var e=e? e : window.event;
var event_element=e.target? e.target : e.srcElement;
alert(event_element.tagName);
}

document.onclick=whatever;
</script>
</head>
<body>
<div>Hey!</div>
</body>
</html>

reazrana
09-17-2007, 06:04 AM
function clickOnElement(srt)
{
var elm_id = "" ;
if (srt==0)
{ elm_id = window.event.srcElement.id; }
else
{ elm_id = "s_" + srt; }
}


call this functon from onclick to trigger the event by clicking on a text in a row(td) table (php)


$tree_string .= "<tr class='td-data-blank'><td idth= 40 ></td><td idth= '100%' id='s_$subroute_id' align='left' onDblClick='load_book_status()' onclick='clickOnElement(0)' class=bsmall style='color:#ffffff;cursor:pointer'>$subroute_name</td></tr>";

jscheuer1
09-17-2007, 06:49 AM
Your function:



function clickOnElement(srt)
{
var elm_id = "" ;
if (srt==0)
{ elm_id = window.event.srcElement.id; }
else
{ elm_id = "s_" + srt; }
}

doesn't do anything in any browser other than set a local variable to only the function itself. Besides, that's just not the way events get handled cross browser, and since you are activating the event from the element itself, there is no need to do it that way, even in IE.

Instead of:


onclick='clickOnElement(0)'

Use:


onclick='clickOnElement(this)'

Then your function could work cross browser like so:


function clickOnElement(srt)
{
var elm_id = "" ;
if (typeof srt=='object')
elm_id = srt.id? srt.id : null;
else if (typeof srt=='string')
elm_id = "s_" + srt;
else
elm_id = null;
}

reazrana
09-17-2007, 06:54 AM
Thanks john

joshuajg
11-20-2010, 01:03 PM
Thank you jscheuer1. Your first example work good for me and is what I was looking for.