By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,725 Members | 1,094 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,725 IT Pros & Developers. It's quick & easy.

An oddity when clicking checkbox

P: n/a
Hi,

Newbish on JS here. Found this script on the web and it does what I want
in FF, but works oddly in IE. When I check the box in IE nothing
happens, but when I click on the body after the box is checked it
displays the dropdown. Any ideas on how to make this work correctly in
IE (displaying the dropdown when box checked)? Thanks for your help.

<html>
<head>
<title>Untitled</title>
<script language="javascript" type="text/javascript">
<!--
function categorychanged(enable) {
if (enable) {
document.form.category_parent.style.display="inlin e";
}
else {
document.form.category_parent.style.display="none" ;
}
}
//-->
</script>
</head>
<body>
<form name="form">
<span>
<input type="checkbox" name="category_level"
onchange="categorychanged(this.checked)">
<select name="category_parent" style="display:none">
<option value="1">1</option>
<option value="2">2</option>
</select>
</span>
</form>
</body>
</html>


--
Patrick A. Smith Assistant System Administrator
Ocean Circulation Group USF - College of Marine Science
http://ocgweb.marine.usf.edu

The trouble with doing something right the first time is that nobody
appreciates how difficult it was. - La Rochefoucauld

Nov 17 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Patrick wrote:
<input type="checkbox" name="category_level"
onchange="categorychanged(this.checked)">
Don't use onchange, use
onclick="categorychanged(this.checked)"
instead.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 17 '08 #2

P: n/a
Martin Honnen wrote:
Patrick wrote:
><input type="checkbox" name="category_level"
onchange="categorychanged(this.checked)">

Don't use onchange, use
onclick="categorychanged(this.checked)"
instead.
Yep, that worked. Thanks Martin. You rock! I figured it was someting as
simple as that, but my limited knowledge of JS precluded me from
figuring that out. Can I ask another question?

What would I do to use the same script over for other checkboxes and
dropdowns so that I wouldn't have to have the same script over and over
with only changing the name="category_level" and name="category_parent"
for each use?

Posted script/form parts again to save you from looking up the earlier
post.

<!--
function categorychanged(enable) {
if (enable) {
document.form.category_parent.style.display="inlin e";
}
else {
document.form.category_parent.style.display="none" ;
}
}
//-->
<form name="form">
<input type="checkbox" name="category_level"
onclick="categorychanged(this.checked)">
<select name="category_parent" style="display:none">
<option value="1">1</option>
<option value="2">2</option>
</select>

--
Patrick

Go Bulls!
Nov 17 '08 #3

P: n/a
Patrick wrote:
Martin Honnen wrote:
>Patrick wrote:
>><input type="checkbox" name="category_level"
onchange="categorychanged(this.checked)">
Don't use onchange, use
onclick="categorychanged(this.checked)"
instead.

Yep, that worked. Thanks Martin. You rock! I figured it was someting as
simple as that, but my limited knowledge of JS precluded me from
figuring that out.
[1]
This is not a matter of (limited) JS knowledge but of (limited) knowledge
about the DOM. The `change' event is supposed to occur when the control's
value changes *and* it loses focus. Clicking/activating a checkbox makes
it change value but does not make it lose focus. So the `onchange' event
handler is an unsuitable one here.
Can I ask another question?
No ;-)
What would I do to use the same script over for other checkboxes and
dropdowns so that I wouldn't have to have the same script over and over
with only changing the name="category_level" and name="category_parent"
for each use?
Since the `click' event bubbles, you would make use of event bubbling.
(See? [^1])

<http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-flow-bubbling>
<http://www.w3.org/TR/DOM-Level-3-Events/events.html#Events-flow>
<http://groups.google.com/groups?as_q=event+bubbling&as_ugroup=comp.lang.jav ascript&scoring=d&filter=0>
[...]
<!--
This does not belong there, remove it.
function categorychanged(enable) {
if (enable) {
document.form.category_parent.style.display="inlin e";
}
else {
document.form.category_parent.style.display="none" ;
}
}
You really should indent the content of Block statements.
//-->
Superfluous; remove that, too.
<form name="form">
The required `action' attribute is missing, the `name' attribute is probably
superfluous.

<http://validator.w3.org/>
<input type="checkbox" name="category_level"
onclick="categorychanged(this.checked)">
<select name="category_parent" style="display:none">
Bad idea. If client-side script support is not present or disabled, and CSS
support is present and enabled, there will be no way for the user to access
that control. You should hide it dynamically with DOM scripting instead, like

<body onload="document.forms[0].elements['category_parent'].style.display
= 'none'">
<option value="1">1</option>
<option value="2">2</option>
It should be possible to omit the `value' attributes in these cases:

<http://www.w3.org/TR/html4/interact/forms.html#h-17.6>
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Nov 17 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.