PDA

View Full Version : Objects with variables?


thejoker
09-25-2002, 06:46 PM
Okay, I have a page full of check boxes and text fields. They have all the same name except for a number change that goes from 1 to 20. I am making a clear button and I would like to do a loop to clear all the fields. Like the following

var x=1;
while (x<=20){
form.box[x].value = '';
form.box[x]name.value = '';
form.box[x]other.checked = false;
...
x++;}

Can this be done?
Thanks :)

beetle
09-25-2002, 08:03 PM
Wait, so you have a form with a bunch of text inputs and checkboxes and whatnot, and you want to clear them all out? why not just use a reset button?

<input type="reset" value="Clear Form">

If you only need to clear some of the controls, then you are on the right track.

Give me some more details and we'll go from there.

thejoker
09-25-2002, 08:41 PM
Its quite a large form and I am making multiple buttons to clear only parts of the form. The buttons will call functions which run the code which was posted previously. :)

<form>
<input type="text" name="box1" />
<input type="text" name="box2" />
...
<submit type="button" onClick="clrInfo(this.form)" />
</form>

function clrInfo(form){
form.box[x].value='';
}

adios
09-26-2002, 03:31 AM
<html>
<head>
<title>untitled</title>
<style type="text/css">

form, input {font-size:12px;}

</style>
<script type="text/javascript" language="javascript">

function clearFields() {
var field;
for (var a=0; arguments[a]; ++a) {
field = arguments[a];
if (field.length) {
for (var grp=field,i=0; i<grp.length; ++i) {
field = grp[i];
if (field.type == 'checkbox' || field.type == 'radio')
field.checked = false;
}
} else {
switch (field.type) {
case 'text' :
case 'textarea' :
field.value = '';
break;
case 'checkbox' :
field.checked = false;
break;
}
}
}
}

</script>
</head>
<body>
<form>
<table><tr><td>
<b>Section 1</b><br>
<input name="t1" type="text"><br>
<input name="t2" type="text"><br>
<input name="c1" type="checkbox"><br>
<input name="c2" type="checkbox"><br>
<b>Section 2</b><br>
<input name="c3" type="checkbox"><br>
<input name="c3" type="checkbox"><br>
<input name="t3" type="text"><br>
<b>Section 3</b><br>
<input name="t4" type="text"><br>
<input name="t5" type="text"><br>
<input name="r1" type="radio"><br>
<input name="r1" type="radio"><br>
<b>Section 4</b><br>
<textarea name="ta1" rows="4" cols="20"></textarea><br>
<input name="t6" type="text"><br>
<input name="c4" type="checkbox"><br>
<input name="t7" type="text"></td><td>
<input type="button" value="CLEAR section 1" onclick="clearFields(t1,t2,c1,c2)"><br>
<input type="button" value="CLEAR section 2" onclick="clearFields(c3,t3)"><br>
<input type="button" value="CLEAR section 3" onclick="clearFields(t4,t5,r1)"><br>
<input type="button" value="CLEAR section 4" onclick="clearFields(ta1,t6,c4,t7)">
</td></tr></table>
</form>
</body>
</html>

thejoker
09-26-2002, 04:35 AM
Wow! That was what I needed!
Thanks!! :thumbsup: