...

View Full Version : Returning <select> result as array... but...



Candrias77
07-06-2005, 09:57 PM
I have a multiple select box in my form with "select all" and "select none" buttons below it. I have named the select box "country_id" which is used in the "select all" and "select none" javascript functions. This is fine.

The problem is that I need PHP to recognise the selected items as an array and hence want to call the box "country_id[]". However when I do this my javascript functions no longer work.

How can I change my javascript functions so that they work with a select box name which ends in []? I have tried a few changes to the javascript but can not get it to work. Below is the code without the [] name.

Select Box:


<select name="country_id" size="10" multiple>
<option value="value">name</option>
...
</select>


Javascript Functions


<script>
function selAll(_v) {
for(var i=0;i<document.delivery.country_id.length;i++)
document.delivery.country_id[i].selected=_v;
}
function selVal(_v) {
for(var i=0;i<document.delivery.country_id.length;i++)
if (document.delivery.country_id[i].value==_v) {
document.delivery.country_id[i].selected=true;
} else {
document.delivery.country_id[i].selected=false;
}
}
function selInv() {
for(var i=0;i<document.delivery.country_id.length;i++)
if (document.delivery.country_id[i].selected==false) {
document.delivery.country_id[i].selected=true;
} else {
document.delivery.country_id[i].selected=false;
}
}
</script>

hemebond
07-06-2005, 10:15 PM
getElementById (http://www.mozilla.org/docs/dom/domref/dom_doc_ref48.html)

Harry Armadillo
07-07-2005, 05:11 AM
function selAll(_v) {
for(var i=0;i<document.forms['delivery']['country_id[]'].length;i++)
document.forms['delivery']['country_id[]'][i].selected=_v;
}Note that using brackets in a name or id is illegal. You can generally get away with it by using array-notation for object indexing, but be on the lookout for unintended side-effects.

Candrias77
07-07-2005, 05:13 AM
Brilliant, thanks for that hemebond.

glenngv
07-07-2005, 06:09 AM
What Harry used is the square bracket notation. You need to learn it as it is more flexible than the dot notation. See my sig for more info.

Candrias77
07-07-2005, 06:23 AM
Will check it out, thanks.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum