Gossamer Forum
Home : General : Internet Technologies :

Javascript - select all "select" values

Quote Reply
Javascript - select all "select" values
Hi,

I'm trying to make a <select> form object automatically select all of the values in it, when clicking on a link.

For example:

Code:
<p>Categories: [ <a href="#" onclick="CheckAll();">select all</a> ]</p>

<select size="9" name="Categories" multiple>
<option value="5">bar</option>
<option value="2">bla bla bkl </option>
<option value="3">dum de dar da</option>
<option value="6">dghfgd</option>
<option value="4">foo</option>
<option value="1">test</option>
</select>

..and then the CheckAll() function would need to select all of the "options" inside the "select" input.

I have some code I've used before for "check-boxes", so they all get ticked - but this doesn't work for a SELECT field. For reference, here is the code that works with checkbox type elements:

Code:
<script language="Javascript">
function CheckAll (box) {
for (var i = document.editor_add.elements.length - 1; i >= 0; i--) {
document.editor_add.elements[i].checked = true;
}
}
</script>

Any suggestions?

TIA!

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: [Andy] Javascript - select all "select" values In reply to
Hi Andy

I've tried to adapt your JavaScript and this seems to work:

Code:

<html>
<head><title>Select-Test</title>

<script type="text/javascript">
function CheckAll () {
for (var i = document.Selecttest.Categories.length - 1; i >= 0; i--) {
document.Selecttest.Categories.options.selected = true;
}
}
</script>

</head>

<body>
<p>Categories: [ <a href="#" onclick="CheckAll();">select all</a> ]</p>

<form name="Selecttest">
<select size="9" name="Categories" multiple>
<option value="5">bar</option>
<option value="2">bla bla bkl </option>
<option value="3">dum de dar da</option>
<option value="6">dghfgd</option>
<option value="4">foo</option>
<option value="1">test</option>
</select>
</form>

</body>

</html>


Have a nice day and all the best
Smileliver

Last edited by:

olivers: Jul 24, 2007, 12:30 AM
Quote Reply
Re: [olivers] Javascript - select all "select" values In reply to
Hi Oliver,

I managed to find a different approach in the end. Nether the less, thanks for the reply :)

Cheers

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!