Gossamer Forum
Home : General : Perl Programming :

Checkbox ticking javascript....

Quote Reply
Checkbox ticking javascript....
Hi. Anyone got an idea on how I could use a bit of javascript to select all of the checkboxes (defined in the code). The main idea is to have a hyuperlinked bit of text that referrences the Javascript function, and then selects all the checkboxes that were defined in the <script> part.

Anyone got any ideas? I'm a total dud on Javascript Frown

Thanks

Andy

BTW: I did have a look for some code on places like javascript.com, but couldnt find anything helpful. URLs would also be helpful :)
Quote Reply
Re: [AndyNewby] Checkbox ticking javascript.... In reply to
Code:
<script language="JavaScript">
<!--
function tickme( form )
{
for( var i = 0; i < form.length; i++ )
if( form.elements.type == 'checkbox' )
form.elements.checked = true;
}
//-->
</script>

<form>
<input type="checkbox">checkbox 1<br>
<input type="checkbox">checkbox 2<br>
<input type="button" value="Tick All" onClick="tickme( this.form )">
</form>
Quote Reply
Re: [AndyNewby] Checkbox ticking javascript.... In reply to
Here is a quick & dirty way to accomplish this.

function CheckBoxes() {
if((document.FormName.chkbox1.checked) && (document.FormName.chkbox2.checked) && (document.FormName.chkbox3.checked)) {
document.FormName.chkbox1.checked = false ;
document.FormName.chkbox2.checked = false ;
document.FormName.chkbox3.checked = false ;
}
else {
document.FormName.chkbox1.checked = true ;
document.FormName.chkbox2.checked = true ;
document.FormName.chkbox3.checked = true ;
}
}


Then call it by hyperlinking text or a graphic like this:
<a href="javascript:CheckBoxes();">

There is also some way to do this by grouping the checkboxes somehow, I believe by giving them a common name? The above isn't the most elegant, but should check all boxes if some are not checked, and deselect all boxes if all are checked. The drawback is you must explicitly declare each checkbox to be affected.

Hope that helps. Smile

--
Matt G
Quote Reply
Re: [mglaspie] Checkbox ticking javascript.... In reply to
No offence but I prefer my way as you don't need to list all the boxes you want to tick.

I guess your way would be ok if you needed to be more selective.

Last edited by:

RedRum: Oct 31, 2001, 6:47 AM
Quote Reply
Re: [RedRum] Checkbox ticking javascript.... In reply to
Thanks Smile

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [RedRum] Checkbox ticking javascript.... In reply to
Quote:
No offence but I prefer my way as you don't need to list all the boxes you want to tick.

I guess your way would be ok if you needed to be more selective.

None taken. I din't realize you could use a "if( form.elements.type == 'checkbox' )" type conditional. I like that. Also, you posted while I was writing mine, so I didn't see your reply till after.

On the other hand, like you said, my solution does allow for picking and choosing which checkboxes to tick, instead of ticking all of them on the form, which may be desirable.

Hmm. I think there may be a way to accomplish this using your method by adding a prefix to the name of each checkbox you want to tick (like "group_ckbx1", "group_ckbx2", etc.) and then using another if statement to check the name of the form element. I'll have to think about that some more.

--
Matt G