Forum Moderators: open

Message Too Old, No Replies

Selecting all options in a multiple select input

Using PHP

         

andytwiz

3:32 pm on May 9, 2006 (gmt 0)

10+ Year Member



Hi,

I'm using PHP to parse returned selected options which means I need to end the select input name with '[]'

This then breaks my javascript which complains about the name ending with [].

(If I remove the [] from the input name the javascript works but then not the PHP).

How do i get around this problem in Javascript?

Cheers


<form name="test_multiple_select_form" method="post">

<select name="test_multiple_select[]" multiple="multiple"><option label="one" value="one">one</option>
<option label="two" value="two">two</option>
</select>

</form>

script type="text/javascript">
<!--
function selectAllList() {
var aSelect = document.test_multiple_select_form.test_multiple_select[];
var aSelectLen = aSelect.length;
for(i = 0; i < aSelectLen; i++) {
aSelect.options[i].selected = true;
}
}
-->
</script>

<a href="javascript:selectAllList()" title="select all">select all</a>

jshanman

3:40 pm on May 9, 2006 (gmt 0)

10+ Year Member



Replace:
var aSelect = document.test_multiple_select_form.test_multiple_select[];
With:
var aSelect = document.test_multiple_select_form["test_multiple_select[]"];

HTH

- JS
[endeavorpub.com...]

ChadSEO

3:42 pm on May 9, 2006 (gmt 0)

10+ Year Member



andytwiz,

You can access it through the form.elements array, like this:

var aSelect = document.test_multiple_select_form.elements[0];

You'll have to change the index, depending on how many inputs are in the form before the <select>.

Chad

andytwiz

3:58 pm on May 9, 2006 (gmt 0)

10+ Year Member



Cheers thats great :D