Forum Moderators: open

Message Too Old, No Replies

unable to pass parameter attachEvent

         

ppaadmin

10:11 am on May 12, 2005 (gmt 0)




Iam creating dynamic rows in my page.Now i need to delete a particular row.
For this iam passing the that particular rowid of the row(dynamically created) to the attachEvent when i click on a particular row.

row.attachEvent("onclick",rowdelete);

this is working fine. but,
when i pass aparameter like,
row.attachEvent("onclick",rowdelete(rowid));
it is throwing a script error: Type Mismatch.

Plz,guide me.

Bernard Marx

1:54 pm on May 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



rowdelete
is (presumeably) a function reference,
rowdelete(rowid)
is a function call. It has the value of whatever is returned by the function, which be no use (type mismatch).

Maybe use the 'classic' form of event assignment:

row.onclick = rowdelete;

Unlike with attachEvent, the keyword, this, will be available as a reference to the row.

function rowdelete()
{
alert(this.rowIndex)
}

-------

Personally, I think it probably better to use event bubbling for this, rather than attaching event handlers to individual rows.

Rambo Tribble

2:47 pm on May 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Could one not, when using the attachEvent technique, employ 'event.srcElement' as a suitable replacement for 'this'? (Of course, this all overlooks that attachEvent is IE-only and parallel addEventListner code would be necessary to accommodate standards-compliant browsers.)

Rambo Tribble

4:04 am on May 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's an example that might be of some use:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<style type="text/css">
div{height:150px;width:300px;}
#divOne{background:#dee;}
#divTwo{background:#eed;}
#divThree{background:#ede;}
</style>
<script type="text/javascript">
function testAtt(evt){
var e_out;
(typeof window.event!="undefined")? e_out=event.srcElement.id : e_out=evt.target.id;
alert(e_out);
}
window.onload=function(){
if(document.addEventListener){ //code for Moz
document.addEventListener('click',testAtt,false);
}else{
document.attachEvent('onclick',testAtt);
}
}
</script>
</head>
<body id="pgBod">
<div id="divOne"></div>
<div id="divTwo"></div>
<div id="divThree"></div>
</body>
</html>

Rambo Tribble

4:28 am on May 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, before Marx gets all picky on me, let me change that to this:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<style type="text/css">
div{height:150px;width:300px;}
#divOne{background:#dee;}
#divTwo{background:#eed;}
#divThree{background:#ede;}
</style>
<script type="text/javascript">
function testAtt(evt){
var e_out;
// target for Moz - srcElement for IE
evt.target? e_out=evt.target.id : e_out=evt.srcElement.id;
alert(e_out);
}
window.onload=function(){
// addEventListener for Moz - attachEvent for IE
document.addEventListener?document.addEventListener('click',testAtt,false):document.attachEvent('onclick',testAtt);
}
</script>
</head>
<body id="pgBod">
<div id="divOne"></div>
<div id="divTwo"></div>
<div id="divThree"></div>
</body>
</html>

Rambo Tribble

1:37 pm on May 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh, now I am embarrassed. This morning it becomes clear: that code would never pass the Bernard Marx inspection - it fails to implement associative array notation for its object property manipulation. Here is my penance:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<style type="text/css">
div{height:150px;width:300px;}
#divOne{background:#dee;}
#divTwo{background:#eed;}
#divThree{background:#ede;}
</style>
<script type="text/javascript">
function testAtt(evt){
var e_out;
var ie_var="srcElement";
var moz_var="target";
var prop_var="id";
// target for Moz, et al. - srcElement for IE
evt[moz_var]? e_out=evt[moz_var][prop_var] : e_out=evt[ie_var][prop_var];
alert(e_out);
}
window.onload=function(){
// addEventListener for Moz, et al. - attachEvent for IE
document.addEventListener?document.addEventListener('click',testAtt,false):document.attachEvent('onclick',testAtt);
}
</script>
</head>
<body id="pgBod">
<div id="divOne"></div>
<div id="divTwo"></div>
<div id="divThree"></div>
</body>
</html>

Bernard Marx

1:52 pm on May 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My nose, on occasion, perhaps.

Of course, this topic has a strong link back to Rambo's thread about onload collision avoidance a few days back. I was, at the time, going to suggest a function that wraps up the various event handler assignment methods

IE: attachEvent
W3C: addEventListener
legacy: "function reference as Javascript property"

Normalising the first two is easy enough (with limitations).

Implementing the third can involve things like continually encapsulating the previous assignment as a call in the newly added one. It is even possible to make sure that events are transmitted using this approach. I know because I had some such code working ..somewhere ..once.

The biggest spanner in the works, in my view, is attacheEvent. I can't see how it is possible for the called function to gain a reference to the originating object. This makes it practically useless for real OO designs.

There is a way, but it involves using a closure, and one which results in a nasty circular reference at that. It's probably useless in loops too.

Rambo's suggestion of using event.srcElement (etc) is good, and will work in 'simple' cases. That is to say, where the element that is assigned the handler contains no sub-elements...

<div>hello <b>you</b></div>

If we use this approach to assign an onclick handler to the DIV, it will fail if the B, or it's textNode, are clicked.

I stick with the 'legacy' approach, because I am at a loss as to what else to do.

Guidance requested.

Rambo Tribble

3:22 pm on May 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, I'd tend to agree with you, Mr. Marx; older is better (at least, that's what I keep trying to convince the ladies of). I was just doing this as a devil's advocacy exercise. Even if a solution were to be found to the problem you describe, I imagine it would be convoluted due to the divergence in the browsers' treatment of event properties.

Rambo Tribble

4:14 pm on May 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Upon further reflection, a plausible ruse occurs: The target elements could be enhanced with a flag property. Upon the firing of the click the function can climb the nodes until it finds one with the flag, then pounce on its prey like a ravenous tribble (When is lunch, anyway?).

Still, more work than with "legacy" techniques. Older is better (no, I'm not balding, I'm becoming more streamlined).