473,472 Members | 1,727 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

An oddity when clicking checkbox

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
3 1839
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tallgeese | last post by:
We developed an in-process COM object for our own application. A toolbar button is used to activate the COM object. But when the COM object loaded, the HTML help hang when clicking the "List...
3
by: Andrew Wrigley | last post by:
Hi I have what seems a corruption of an mdb that is not solved by the decompile command line trick. The problem appears when clicking on a subform control (check box) that has a event handler...
0
by: jeff | last post by:
is there a way to display an hourglass for the client's cursor when clicking a button running server-side code? thanks.
4
by: James Brett | last post by:
I have one form on the page but have multiple submit buttons for different elements. When clicking on a button not associated with the required elements the validator popup still comes up. How...
2
by: André | last post by:
Hi, I made a detailsview containing several fields for inserting only. I also made a dropdownlist which is invisible at start. Now, when clicking in a particular textbox, the dropdownlist must...
1
by: karups | last post by:
Hi I have binded a dataset to my datagrid. Datagrid is present inside a >DIV></DIVtag of specific height. Only 5 records will be visible at a time when i edit the 10th record, the page...
0
by: karups | last post by:
Hi I have binded a dataset to my datagrid. Datagrid is present inside a >DIV></DIVtag of specific height. Only 5 records will be visible at a time when i edit the 10th record, the page...
3
by: BaseballGraphs | last post by:
Hello, I am looking to disable all checkboxes when one checkbox is selected. I cannot use radio buttons, so I need to be able to disable the checkboxes if 1 checkbox has been selected. Can you...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.