Forum Moderators: open

Message Too Old, No Replies

wildcards?

how do I use 'em in javascript

         

mistafeesh

11:10 am on Apr 6, 2004 (gmt 0)

10+ Year Member



I'm pretty noob-ish when it comes to javascript, I'm afraid...

I have a small on a php-generated page, which ticks a set of checkboxes on the press of a button. The problem is that the checkbox names are generated from the database.

Until I had to change the field names, the following worked:

onClick="checkAll(document.form1.Teignbridge)"

but now I've had to name them "Teignbridge1, Teignbridge2, etc.

Can I use a wildcard in order to tell it to click all fields that begin with "Teignbridge"?

Alternative Future

12:30 pm on Apr 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi mistafeesh,

This should help you out, by adding [checkbox1] to your static input tags name after your variable, then calling on the function.

<script language="javascript">
function selectUnselectAll(grp, button) {
var form, el, e, f = 0;
var unbuttoned = (button.value.substring(0,2).toLowerCase() == 'un');
while (form = document.forms[f++]) {
e = 0;
while (el = form.elements[e++])
if (el.type == 'checkbox' && el.name.indexOf(grp)!= -1) el.checked =!unbuttoned;
}
button.value = unbuttoned? button.value.substring(2) : 'un' + button.value;
}
</script>

<input type="checkbox" name="Teignbridge1[checkbox1]">Teignbridge1<br>
<input type="checkbox" name="Teignbridge2[checkbox1]">Teignbridge2<br>
<input type="checkbox" name="Teignbridge3[checkbox1]">Teignbridge3<br>
<input type="checkbox" name="Teignbridge4[checkbox1]">Teignbridge4<br><br>
<input type="checkbox" onclick="selectUnselectAll('checkbox1',this)">Type1

HTH,

-George

mistafeesh

4:34 pm on Apr 6, 2004 (gmt 0)

10+ Year Member



I ended up solving this particular problem in PHP, by creating a list of all the checkboxes that needed checking. However, I still can't get the script to actually work...I'm off to tweak it and see what I'm doing wrong...

Alternative Future

4:37 pm on Apr 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

Was there a problem with the script that I supplied?

-George