Forum Moderators: open
I'm thinking the easiest would be if I can get the name of the control clicked - something like if checkbox was clicked don't worry about the swap b/c the checkbox takes care of itself, but if the div was clicked then swap it. But, since the div onClick fires either way, that's causing a problem.
Thanks,
- Ryan
The W3C-sanctioned method is stopPropagation(). Naturally, you could branch your code to accommodate browsers that can't handle cancelBubble, but support stopPropagation().
Either thing I click on (checkbox or it's parent div) fires the onClick for the div. In the function that's called by onClick, is there a way to tell whether the checkbox or the div was clicked?
<!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">
</head>
<body>
<form action="">
<div style="height:200px;width:300px;background:#dee;" onclick="alert(this.tagName);">
<input type="checkbox" onclick="event.cancelBubble=true;" /><br /><br />
<input type="checkbox" onclick="event.cancelBubble=false;" />
</div>
</form>
</body>
</html>
One additional question though: I'm passing to one of the functions as function_name(event) under the onClick. Is there a way to get the event if I pass it as function_name(this) instead? Just seems cleaner, although it's working great as-is.
- Ryan
Thanks to everyone who helped
- Ryan
I haven't ever used <label>, but I thought about <span>ing the text.
You really should try it. Without knowing exactly what you want to do, I can't be certain, but I think you would be able to ditch all that javascript. You might need to use a little css too.
<label style="display:block">
<input type="checkbox" name="ck_test" onclick="testClick()">
<span style="color:#000080">Test</span>
</label>
I haven't use display:block with a label but it should be ok. This will make the <label> behave like a div, however, if you place block elements in it, it will fail to validate.
Kaled.