Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checkbox function not working in Firefox

Status
Not open for further replies.

Zipster

Programmer
Nov 13, 2000
107
GB
Hi,

I am trying to use the following function to tick a checkbox if it is in a false state, or untick in a true state. e.g:

<input name="Area1" type="checkbox" value="Africa">
<a href="#" onClick="CheckMyBox(Area1);return false;">AFRICA</a>



function CheckMyBox(chk)
{
if(chk.checked)
{
chk.checked=false;
}
else
{
chk.checked=true;
}
}

Page is here:
Works in IE not Firefox. Thanks for any help.

K Abbott IT Manager / Web Developer

 
Hi

Where are defined the Area1, Area2 and Area3 variables ?

Why do you need JavaScript anyway ? HTML is enough :
HTML:
[b]<label><input[/b] [maroon]name[/maroon][teal]=[/teal][green][i]"Area1"[/i][/green] [maroon]type[/maroon][teal]=[/teal][green][i]"checkbox"[/i][/green] [maroon]value[/maroon][teal]=[/teal][green][i]"Africa"[/i][/green][b]>[/b]AFRICA[b]</label>[/b]

Feherke.
 
[&nbsp;]

I don't know if the following will solve your problem or not. However, functions should be SELF-CONTAINED and NOT rely on variables OUTSIDE of the function. Therefore, I would rewrite your function along these lines:

[tt][blue]
function CheckMyBox(chk) {
check = true;

if(chk) {
check = false;
}

return check;
}
[/blue][/tt]


Not sure if all of the javascript syntax is correct as I don't use javascript very often. However, the basic logic is correct, so you can tinker from there.

If you wanted to get really creative this function could be reduced to the form

[tt][blue]
function CheckMyBox(chk) {
return {{formula}};
}
[/blue][/tt]


where [tt][blue]{{formula}}[/blue][/tt] is a formula for choosing "true" or "false".

mmerlinn


"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Steven Raymond
 
Why Javascript? When this is already supported in most browsers natively, with the use of a label:

Code:
<input type=checkbox name="opt1" id="opt1"><label for="opt1" style="cursor:pointer;">Option 1</label>

No need to re-invent the wheel if its already been done for you.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hi

But nobody wants nor needs to return anything there.

And if you want to be "creative", then no [tt]if[/tt] is needed there :
JavaScript:
[b]function[/b] [COLOR=darkgoldenrod]CheckMyBox[/color][teal]([/teal]chk[teal])[/teal]
[teal]{[/teal]
  chk[teal].[/teal]checked[teal]=![/teal]chk[teal].[/teal]checked[teal];[/teal]
[teal]}[/teal]

Feherke.
 
[&nbsp;]

feherke

Your solution, though correct, still relies on the outside variable "checked". By removing this reliance, the function can be used anywhere with any variable.

mmerlinn


"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Steven Raymond
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top