Forum Moderators: open

Message Too Old, No Replies

Fire just by one handler

There is a checkbox in a (whatever) TR tag

         

gergo

1:48 am on Jan 28, 2007 (gmt 0)

10+ Year Member



Hi guys,

is there anybody who has an idea what to do with this?:

I have an element. TR. It has a chekbox inside. When I define an onclick on the tr tag, it works fine.

But when I click on the checkbox which is inside the tr, after handling the "check" event, the onclick event starts for the tr tag also, so the page will be directed to the location.

How could I prevent the TR onclick event for the checkbox?

Any idea would be apprreciated very much!

Gregor

<td onclick="document.location.href='url.html';">
.....
<input type="checkbox"
.....
</td>

daveVk

2:38 am on Jan 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<input type="checkbox" onclick="return false;" >

This is worth a try, however this may bypass normal onclick handling of checkbox, you may need extra code prior to 'return false;'

Welcome to forum

gergo

1:15 pm on Jan 28, 2007 (gmt 0)

10+ Year Member



Hi Dave!

Thanks for the response, but it does not solves the problem. Here is it exactly. When you click inside the box, the message appears. When you click on the checkbox, then the message also appears, because the chekcbox is also inside the tr tag. I would like to avoid the handling of onclick event for the tr tag when I click the chekbox.

Guys, does anybody has any other idea?

<table>
<tr onmouseover="this.style.backgroundColor='#ff0000;'" onmouseout="this.style.backgroundColor='#ffffff;'"
onclick="alert('I hope this came outside of the checkbox...');">

<td style="width:200px; height:50px; border:1px solid #000000;">

<input type="checkbox" onclick="return false;">

</td>
</tr>
</table>

geofflee

3:37 pm on Jan 28, 2007 (gmt 0)

10+ Year Member



This behavior is actually browser dependent. It is known as event bubbling and event capturing. Quirksmode has a wonderful article that explains how all of this works: [quirksmode.org ]

mrhoo

4:06 pm on Jan 28, 2007 (gmt 0)

10+ Year Member



You don't really want to return false, or you can't get the checkbox to be checked.
You want to stop the event bubbling up from the checkbos- and there are two implementations for this, IE's cancelBubble property and everybody else's stopPropagation.

This does the trick.

<html>
<head>
<title>Stop Bubbling</title>
<script type="text/javascript">

if(document.implementation && document.implementation.hasFeature &&
document.implementation.hasFeature('HTML','')){

onload=function(){
var A=document.getElementsByTagName('tr');
var L=A.length,hoo,cb;
var str=('This did not come from the checkbox...');
for(var i=0;i<L;i++){
hoo=A[i];
hoo.onmouseover=function(e){
e= (window.event)? event.srcElement: e.target;
e.style.backgroundColor='red';
}
hoo.onmouseout=function(e){
e= (window.event)? event.srcElement: e.target;
e.style.backgroundColor='white';
}
hoo.onclick=function(){alert(str)};
cb=hoo.getElementsByTagName('input')[0];
if(cb)cb.onclick= function(e){
if(window.event) event.cancelBubble= true;
else if(e.cancelable)e.stopPropagation();
}
}
}
}
</script>
</head>
<body>
<table style="width:200px;border:1px solid #000000;padding:0" cellspacing="0">
<tbody>
<tr >
<td style="width:200px;height:50px;;border:1px solid #000000 ">
<input type="checkbox" >
</td>
</tr>
</tbody>
</table>
</body>
</html>

daveVk

11:35 pm on Jan 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Rather than canceling event propagation it may be easier to check source of click.

function trClick(e) {
var e= (window.event)? event.srcElement: e.target;
if ( e.tagName!= 'INPUT' ) {alert('I hope this came outside of the checkbox...'); }
}

gergo

6:46 pm on Feb 1, 2007 (gmt 0)

10+ Year Member



Guys,
thanks for the answers!

Cancelling the events seems to be professional, but I like best the last solution. I will try it soon. - Thanks Dave!

G.