|
|||||||
| JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...) |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Disable Checkbox array
Hey guys, im trying to disable a checkbox array when I clock on a certain checkbox, but i can't seem to get the script to work.
Here is the html, I didn't provide the script because its a _very_ big mess. Thanks guys in advance Code:
<FORM> <INPUT TYPE=CHECKBOX NAME="head" onClick="disable(this.form)">head<P> <INPUT TYPE=CHECKBOX NAME="headitem[]" VALUE="ip">ip<BR> <INPUT TYPE=CHECKBOX NAME="headitem[]" VALUE="host">host<BR> <INPUT TYPE=CHECKBOX NAME="headitem[]" VALUE="name">name<BR> <INPUT TYPE=CHECKBOX NAME="headitem[]" VALUE="id">id <P><INPUT TYPE=SUBMIT VALUE="submit"> |
|
#2
|
||||
|
||||
|
Checkboxes require differrent names. Make them unique and then try:
Code:
function disable (f) {
var e, i = 1;
while (e = f.elements[i++]) {if (e.type.toLowerCase() == 'checkbox') e.disabled = true}
}
Code:
function disable (f) {
var e, i = 0;
while (e = f.getElementsByName ('headitem[]')[i++]) {e.disabled = true}
}
__________________
“The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.” —Tim Berners-Lee, W3C Director and inventor of the World Wide Web Last edited by Charles; 09-29-2005 at 04:24 PM. |
|
#3
|
|||
|
|||
|
Here's another way.
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title></title>
<script type="text/javascript"><!--
// IE4+, Firefox, Netscape 6+, Opera 6+
function disable(cbx) {
if(!cbx) return;
var frm = cbx.form;
for(var i=0;i<frm.elements.length;i++) {
if(frm.elements[i].type && (frm.elements[i] != cbx)) {
if(frm.elements[i].type.toLowerCase() == 'checkbox') {
frm.elements[i].disabled = cbx.checked;
}
}
}
}
// -->
</script>
</head>
<body>
<form name="form1" action="">
<input type="checkbox" name="head" onclick="disable(this)">head<br><br>
<input type="checkbox" name="headitem[]" value="ip">ip<br>
<input type="checkbox" name="headitem[]" value="host">host<br>
<input type="checkbox" name="headitem[]" value="name">name<br>
<input type="checkbox" name="headitem[]" value="id">id
<p><input type="submit" value="submit"></p>
</form>
</body>
</html>
Quote:
__________________
Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around. Check out my blog. Last edited by Kravvitz; 09-29-2005 at 04:36 PM. |
|
#4
|
||||
|
||||
|
I stand corrected. Thank you.
From the HTML 4.01 Specification: Quote:
__________________
“The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.” —Tim Berners-Lee, W3C Director and inventor of the World Wide Web |
|
#5
|
|||
|
|||
|
thanks Kravvitz, that was exactly what i was looking for.
Any way i could reverse the process? (check enables them, and uncheck disables). Also, is it possible to make the head an array also? So lets say, I have multiple forms, but if each *head* is checked, it would enable that certain check box, and pass on with postdata that the header is checked, and each individual box is checked. Sound confusing? hehe, let me explain the whole project. I'm making a database site, with multiple search critirias. I want to enable each table to be selected within the mysql query, but in order to do that, i have to combine the tables, thus adding the checkbox's into an array. Basicly it will look something like this. Head() 1name() 1name() 1name() 1name() Head() 2name() 2name() 2name() 2name() Head() 3name() 3name() 3name() 3name(). I realize i went very off track on this post, but I am just l ooking for guidance, I am a PHP\JS beginner, but I seem to be getting the hang of it =) Thanks guys. *edit* after i looked at my post, i realized I'm being very greedy. If its possible to just show me how to reverse the check I would appreciate it. *edit* Last edited by endiz; 09-29-2005 at 05:12 PM. |
|
#6
|
|||
|
|||
|
You're welcome
![]() Quote:
Code:
frm.elements[i].disabled = cbx.checked; Code:
frm.elements[i].disabled = !cbx.checked; Quote:
__________________
Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around. Check out my blog. |
|
#7
|
|||
|
|||
|
I love you buddy. I wish I could kiss you.
Thank you very much. Much appreciate it. (both arrays work too). Last edited by endiz; 09-29-2005 at 05:58 PM. |
|
#8
|
|||
|
|||
|
Hey Krav, few more questions...
What if i have multiple sections per form, and i want to disable each section per head checkbox. I tried to change your code around, but i seemed to just break it. the form now looks like this. Code:
<form name="form1"> <input type="checkbox" name="table[]" onclick="disable(this,head)" value="head">head<br><br> <input type="checkbox" name="headitem[]" value="ip" DISABLED>ip<br> <input type="checkbox" name="headitem[]" value="host" DISABLED>host<br> <input type="checkbox" name="headitem[]" value="name" DISABLED>name<br> <input type="checkbox" name="headitem[]" value="id" DISABLED>id <input type="checkbox" name="table[]" onclick="disable(this,nva)" value="nva">nva<br><br> <input type="checkbox" name="headitem[]" value="name" DISABLED>name<br> <input type="checkbox" name="headitem[]" value="date" DISABLED>date<br> <input type="checkbox" name="headitem[]" value="analyzed" DISABLED>analyzed<br> <input type="checkbox" name="headitem[]" value="email" DISABLED>email <p><input type="submit" value="submit"></p> </form> Code:
<script type="text/javascript"><!--
// IE4+, Firefox, Netscape 6+, Opera 6+
function disable(cbx) {
if(!cbx) return;
var frm = cbx.form;
for(var i=0;i<frm.elements.length;i++) {
if(frm.elements[i].type && (frm.elements[i] != cbx)) {
if(frm.elements[i].type.toLowerCase() == 'checkbox') {
frm.elements[i].disabled = !cbx.checked;
}
}
}
}
// -->
</script>
Last edited by endiz; 09-30-2005 at 10:20 AM. |
|
#9
|
||||
|
||||
|
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta content="text/css" name="Content-Style-Type">
<meta content="text/javascript" name="Content-Script-Type">
<title>Example</title>
<script type="text/javascript">
function toggle (o) {
var e, i = 0
while (e = o.parentNode.parentNode.getElementsByTagName ('INPUT')[i++]) {if (e.name == 'headitem[]') e.disabled = o.checked}
}
</script>
<style type="text/css">
form {width:24em; margin:auto}
fieldset {margin:1ex; padding:1ex; width:10em}
label {display:block}
label.disable {margin-bottom:1em}
button {display:block; margin:auto}
</style>
</head>
<body>
<form action="some-script.pl">
<fieldset>
<legend>Head</legend>
<label class="disable"><input type="checkbox" name="table[]" onclick="toggle (this)" value="head">Disable</label>
<label><input type="checkbox" name="headitem[]" value="ip">ip</label>
<label><input type="checkbox" name="headitem[]" value="host">host</label>
<label><input type="checkbox" name="headitem[]" value="name">name</label>
<label><input type="checkbox" name="headitem[]" value="id">id</label>
</fieldset>
<fieldset>
<legend>Nva</legend>
<label class="disable"><input type="checkbox" name="table[]" onclick="toggle (this)" value="head">Disable</label>
<label><input type="checkbox" name="headitem[]" value="ip">ip</label>
<label><input type="checkbox" name="headitem[]" value="host">host</label>
<label><input type="checkbox" name="headitem[]" value="name">name</label>
<label><input type="checkbox" name="headitem[]" value="id">id</label>
</fieldset>
<div><button type="submit">Button</button></div>
</form>
</body>
</html>
__________________
“The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.” —Tim Berners-Lee, W3C Director and inventor of the World Wide Web |
|
#10
|
|||
|
|||
|
Perfect!
Thanks guys! |
|
#11
|
|||
|
|||
|
Got another question:
How would I implement another toggle script where it would disable and enable the appropriate textbox assossciated with that checkbox. Here is my code so far. Thanks guys, I'm learning alot. Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta content="text/css" name="Content-Style-Type">
<meta content="text/javascript" name="Content-Script-Type">
<title>TEST</title>
<script type="text/javascript">
function toggle (o) {
var e, i = 0
while (e = o.parentNode.parentNode.getElementsByTagName ('INPUT')[i++]) {if (e.name == 'headitem[]') e.disabled = !o.checked}
}
</script>
<style type="text/css">
form {width:24em; margin:auto}
fieldset {margin:1ex; padding:1ex; width:20em}
label {display:block}
label.disable {margin-bottom:1em}
button {display:block; margin:auto}
</style>
</head>
<body>
<form action="advresult.php">
<fieldset>
<legend>Head<input type="checkbox" name="table[]" onclick="toggle (this)" value="head"></legend>
<label><input type="checkbox" name="headitem[]" value="ip" DISABLED>ip<input type="text" name="query[]" maxlength=25 DISABLED></label>
<label><input type="checkbox" name="headitem[]" value="host" DISABLED>host<input type="text" name="query[]" maxlength=25 DISABLED></label>
<label><input type="checkbox" name="headitem[]" value="name" DISABLED>name<input type="text" name="query[]" maxlength=25 DISABLED></label>
<label><input type="checkbox" name="headitem[]" value="id" DISABLED>id<input type="text" name="query[]" maxlength=25 DISABLED></label>
</fieldset>
<fieldset>
<legend>Nva<input type="checkbox" name="table[]" onclick="toggle (this)" value="NVA"></legend>
<label><input type="checkbox" name="headitem[]" value="name" DISABLED>name<input type="text" name="query[]" maxlength=25 DISABLED></label>
<label><input type="checkbox" name="headitem[]" value="date" DISABLED>date <input type="text" name="query[]" maxlength=25 DISABLED></label>
<label><input type="checkbox" name="headitem[]" value="by" DISABLED>by <input type="text" name="query[]" maxlength=25 DISABLED></label>
<label><input type="checkbox" name="headitem[]" value="analyzed" DISABLED>analyzed <input type="text" name="query[]" maxlength=25 DISABLED></label>
</fieldset>
<div><button type="submit">Search</button></div>
</form>
</body>
</html>
|
|
#12
|
|||
|
|||
|
Would i use the oncheck event? or still use the onclick event...
|
|
#13
|
|||
|
|||
|
Quote:
Quote:
__________________
Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around. Check out my blog. |
|
#14
|
|||
|
|||
|
I believe this is the pair.
Code:
<label><input type="checkbox" name="headitem[]" value="ip" DISABLED>ip<input type="text" name="query[]" maxlength=25 DISABLED></label> |
|
#15
|
|||
|
|||
|
You should not have multiple form controls associated with a single <label>.
Quote:
Try this. I added the style="display:inline;" because you have the <label>s set to display:block. Code:
<div><input type="checkbox" name="headitem[]" value="ip"
onclick="this.parentNode.getElementsByTagName('input')[1].disabled=this.checked;"
DISABLED><label style="display:inline;">ip<input type="text" name="query[]" maxlength=25 DISABLED></label></div>
__________________
Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around. Check out my blog. |
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|