Connecting Tech Pros Worldwide Help | Site Map

An oddity when clicking checkbox

  #1  
Old November 17th, 2008, 04:45 PM
Patrick
Guest
 
Posts: 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

  #2  
Old November 17th, 2008, 05:15 PM
Martin Honnen
Guest
 
Posts: n/a

re: An oddity when clicking checkbox


Patrick wrote:
Quote:
<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/
  #3  
Old November 17th, 2008, 06:55 PM
Patrick
Guest
 
Posts: n/a

re: An oddity when clicking checkbox


Martin Honnen wrote:
Quote:
Patrick wrote:
>
Quote:
><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!
  #4  
Old November 17th, 2008, 07:25 PM
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a

re: An oddity when clicking checkbox


Patrick wrote:
Quote:
Martin Honnen wrote:
Quote:
>Patrick wrote:
Quote:
>><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.
Quote:
Can I ask another question?
No ;-)
Quote:
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>
Quote:
[...]
<!--
This does not belong there, remove it.
Quote:
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.
Quote:
//-->
Superfluous; remove that, too.
Quote:
<form name="form">
The required `action' attribute is missing, the `name' attribute is probably
superfluous.

<http://validator.w3.org/>
Quote:
<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'">
Quote:
<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
Closed Thread