Javascript to check checkboxes?

P

Paul Rubin

Hi all. I'm wondering if there's a way to have checkboxes become
selected without clicking the mouse. Perhaps if you rollover the
checkboxes, they could toggle checked and unchecked? Or perhaps you
could click-and-hold and then drag across a series of checkboxes to
select them?

Please let me know,
Sincerely,
Paul Rubin
(e-mail address removed)
 
M

McKirahan

Paul Rubin said:
Hi all. I'm wondering if there's a way to have checkboxes become
selected without clicking the mouse. Perhaps if you rollover the
checkboxes, they could toggle checked and unchecked? Or perhaps you
could click-and-hold and then drag across a series of checkboxes to
select them?

Please let me know,
Sincerely,
Paul Rubin
(e-mail address removed)


Like the following? Watch for word-wrap.

<html>
<head>
<title>checkme.htm</title>
<script language="javascript" type="text/javascript">
<!--
function mouser() {
var form = document.forms[0];
if (form.box1.checked) {
form.box1.checked = false;
} else {
form.box1.checked = true;
}
}
// -->
</script>
</head>
<body>
<form>
<input type="checkbox" name="box1" value="1" onmouseover="mouser()">
</form>
</body>
</html>
 
J

Janwillem Borleffs

McKirahan said:
function mouser() {
var form = document.forms[0];
if (form.box1.checked) {
form.box1.checked = false;
} else {
form.box1.checked = true;
}
}

Or:

function mouser() {
var theBox = document.forms[0].elements['box1'];
theBox.checked = !theBox.checked;
}

Depending on which syntax you prefer...


JW
 
P

Paul Rubin

Thanks for your kind reply. How about if there are a whole bunch of
checkboxes, with names, p.1, p.2, p.3, etc ???

I hope this can be incorporated!
Sincerely,
Paul Rubin
(e-mail address removed)

McKirahan said:
Paul Rubin said:
Hi all. I'm wondering if there's a way to have checkboxes become
selected without clicking the mouse. Perhaps if you rollover the
checkboxes, they could toggle checked and unchecked? Or perhaps you
could click-and-hold and then drag across a series of checkboxes to
select them?

Please let me know,
Sincerely,
Paul Rubin
(e-mail address removed)


Like the following? Watch for word-wrap.

<html>
<head>
<title>checkme.htm</title>
<script language="javascript" type="text/javascript">
<!--
function mouser() {
var form = document.forms[0];
if (form.box1.checked) {
form.box1.checked = false;
} else {
form.box1.checked = true;
}
}
// -->
</script>
</head>
<body>
<form>
<input type="checkbox" name="box1" value="1" onmouseover="mouser()">
</form>
</body>
</html>
 
M

McKirahan

Paul Rubin said:
Thanks for your kind reply. How about if there are a whole bunch of
checkboxes, with names, p.1, p.2, p.3, etc ???

I hope this can be incorporated!
Sincerely,
Paul Rubin
(e-mail address removed)

The following does what you want.
(I've used JW's elegant approach.)
Watch for word-wrap.

<html>
<head>
<title>checkers.htm</title>
</head>
<body>
<form>
<input type="checkbox" name="p.1" value="One"
onmouseover="this.checked=!this.checked">
<input type="checkbox" name="p.2" value="Two"
onmouseover="this.checked=!this.checked">
<input type="checkbox" name="p.3" value="Three"
onmouseover="this.checked=!this.checked">
<input type="checkbox" name="p.4" value="Four"
onmouseover="this.checked=!this.checked">
</form>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,152
Messages
2,570,857
Members
47,398
Latest member
Moorcam

Latest Threads

Top