Forum Moderators: open

Message Too Old, No Replies

eventBubbling - MISSING target id

When I try to get the target or srcElement id, it works for

         

gergo

6:07 pm on Feb 18, 2007 (gmt 0)

10+ Year Member



Hi Everybody,

Does anybody have any clue, why can't I get the target id for a tr element? I use the same code for an input element, and it works fine. Here is the code.
If you click on the checkbox, it will alert it's id, and eventBubbling has cancelled, so no other handler starts - that is fine.

BUT, when you click on the "Simple cell", The alert comes up, I can se the HTML object as well, but it says, it has no ID! Any idea?
Maybe I will pass it as parameter...

Gergo

<html>
<head></head>
<body>

<table>
<tr id="row_id_1">
<td width="50"><input type="checkbox" id="chkbx_id_1" value="1"></td>
<td width="50">Simple cell</td>
</tr>
</table>

<script>
function rowHandler(e) {
if (!e) var e = window.event;

if (e.target) targ = e.target.id;
if (e.srcElement) targ = e.srcElement.id;

alert('From TR ' + e.type + ' ' + targ);
}

function cellHandler(e) {
if (!e) var e = window.event;

e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation();

if (e.target) targ = e.target.id;
if (e.srcElement) targ = e.srcElement.id;

alert('From TD ' +e.type + ' ' + targ);

}

var oRow_1= document.getElementById('row_id_1');
oRow_1.onclick = rowHandler;
if (oRow_1.captureEvents) oRow_1.captureEvents(Event.CLICK);

var cBox_1= document.getElementById('chkbx_id_1');
cBox_1.onclick = cellHandler;
if (cBox_1.captureEvents) cBox_1.captureEvents(Event.CLICK);
</script>
</body>
</html>

scriptmasterdel

10:23 pm on Feb 18, 2007 (gmt 0)

10+ Year Member



Your current element / object is a td without an id, so you need to focus on the parentNode and that is the tr.

so ....

targ = e.target.id;

... should be ...

targ = e.target.parentNode.id;

I hope this helps.

Del

gergo

10:34 am on Mar 4, 2007 (gmt 0)

10+ Year Member



Hi Del - and everybody, who reads it,

you are right Del, but the solution was that there is a currentTarget property also besides the target, which gives it back correctly.

Thanks for your response.

Gergo