473,396 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1837
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.