/    Sign up×
Community /Pin to ProfileBookmark

JavaScript checkbox problem; works in firefox but not in ie7

Can anyone explain why this script works in firefox but not ie7?

“Does Not Apply” should get unchecked if the user chooses any other options or types one in manually.

All other boxes should uncheck and the text field should clear if “Does Not Apply” gets checked.

Example: [url]http://www.olfactoryhues.com/temp/test.htm[/url]

My simplified code:

[CODE]
<html>
<head>
<title>test</title>
<script language=’JavaScript’>
function UncheckDNA(){
document.fruits.fruits[0].checked=false;
}

function UncheckALL(){
for (i = 1; i < document.fruits.fruits.length; i++) {
document.fruits.fruits[i].checked = false ;
document.fruits.NewFruit.value = ”;
}
}
</script>
</head>
<body>
<form name=’fruits’>
<input type=’checkbox’ id=’fruits’ name=’fruits[]’ value=” onchange=’UncheckALL();’ checked>Does Not Apply<br>
<input type=’checkbox’ id=’fruits’ name=’fruits[]’ value=’Apple’ onchange=’UncheckDNA();’>Apple<br>
<input type=’checkbox’ id=’fruits’ name=’fruits[]’ value=’Banana’ onchange=’UncheckDNA();’>Banana<br>
<input type=’checkbox’ id=’fruits’ name=’fruits[]’ value=’Orange’ onchange=’UncheckDNA();’>Orange<br>
<input type=’text’ name=’NewFruit’ size=’15’ onchange=’UncheckDNA();’>
<input type=’submit’ value=’Add’><br><br>
<input type=’submit’ value=’Submit’>
</form>
</body>
</html>
[/CODE]

Any assistance you can offer would be greatly appreciated!

-Jack

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@FangJan 31.2007 — id must be unique

form and control [I]name[/I] are the same which can confuse the browser.

Not [I]onchange[/I], but [I]onclick[/I], except for 'NewFruit' which should be [I]onkeyup[/I]
Copy linkTweet thisAlerts:
@slaughtersJan 31.2007 — Is the ID even being used in this case? It looks to me like the form element is being accessed, not the ID

I use a more generic unCheck function myself. It looks like this
[code=html]function UncheckALL(oElement) {
oForm = oElement.form;
oElement = oForm.elements[oElement.name];
if(oElement.length) {
for(var i = 0; i < oElement.length; i++) { oElement[i].checked = false; }
}
} [/code]


You call it from a form element using "this" as a parameter, ie

<input type=checkbox id="blahblah" name="blahblah" onClick="UncheckALL(this)";>


Note: You can make the following change to the if statement if you want it to toggle all the checkboxes off/on
[code=html]if (oElement.length) {
bChecked = oElement[0].checked; // Assuming the first checkbox is the "toggle" check box control
for(i = 1; i < oElement.length; i++) {oElement[i].checked = bChecked;}
}[/code]
Copy linkTweet thisAlerts:
@Jack_StrawauthorJan 31.2007 — awesome, that did the trick. Thanks fang!
Copy linkTweet thisAlerts:
@FangJan 31.2007 — Is the ID even being used in this case?[/QUOTE]It was an observation; a common mistake that requires correcting.
Copy linkTweet thisAlerts:
@slaughtersJan 31.2007 — It was an observation; a common mistake that requires correcting.[/QUOTE]Yes, it is common, as I thought multiple instances of the same ID would be treated the same way as multiple instances of the same name in a form is treated (ie an array of that name is created with element 0 being the first item)

The weird thing is that correcting it seems to have fixed Jack.Straw's problem, even though the ID was never referenced in his JavaScript.
×

Success!

Help @Jack_Straw spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...