views:

112

answers:

4

have an array of values [1,2,4] representing the values of a multi-select box, how do i set them as selected using either jquery or plain javascript?

thanks for your help.

A: 

I'm not very familiar with Javascript, but i think it's this:

var values = [1, 2, 4];
var sel = document.getElementsByTagName('SELECT')[0];
for (var i = 0; i < sel.options.length; i++) {
    if(sel.options[i].value == 1 || sel.options[i].value == 2 || sel.options[i].value == 4)
        sel.options[i].selected = 'selected';
}
Gabriel Guimarães
+3  A: 

In plain JavaScript:

function in_array(needle, haystack, argStrict)
{
    var key = '', strict = !!argStrict;
    if (strict)
    {
        for (key in haystack)
        {
            if (haystack[key] === needle)
                return true;
        }
    }
    else
    {
        for (key in haystack)
        {
            if (haystack[key] == needle)
                return true;
        }
    }
    return false;
}

var values = new Array(1, 2, 4);
var select = document.getElementById('selectName'); //Change to the id of the select
if (select)
{
    for (var i = 0; i < select.options.length; i++) 
    {
        //Select options matching array values, unselect others
        select.options[i].selected = in_array(select.options[i].value, values, false);
    }
}

UPDATE: Added JavaScript function in_array that mimic the PHP one...

AlexV
for each value on the array you will loop through the entire collection of items in the select? aren't you overdoing it?
Gabriel Guimarães
Yes it's a bit overkill, but since many elements in the select can have the same value... It's unlikely, but it's possible :) Anyway with this snippet I guess you get the idea how to do it.
AlexV
I don't think that the developer should allow a select with the same value as other, that would be maintainability hell.
Gabriel Guimarães
In some case yes, in some other no. For example, a country list. You have all the country of the world sorted alphabetically. At the top of the list you often have duplicates representing the most used choices like USA, Canada...
AlexV
Sorry if you have duplicate items on the select, I don't understand why would you sort them alphabetically.
Gabriel Guimarães
Have you never saw a list of all the countries of the world? They are hundreds of them, all sorted alphabetically. But often at the beggining of the list you will have the most frequent choices for your region followed by a separator. Something like USA, Canada, [separator], Afghanistan, South Africa, ... Cambodia, Cameroon, Canada...
AlexV
+1  A: 

Since I do not yet have 50 rep points I cannot comment or fix the post above

So here is my fix to the above array

var values = new Array(1, 2, 4); // using commas
var values = [1, 2, 4]; // using array notation 
mplungjan
- ah, he fixed it himself...
mplungjan
However Harmen is wrong.You cannot select an option on its index using its value like you posted
mplungjan
+5  A: 

jQuery has a utility called $.inArray(value,array). You could do something like this:

var array = [1,2,4];

$('#example option').each(function() {
    var $th = $(this);
    var value = parseInt($th.val());
    if( $.inArray(value,array) >= 0 ) {
        $th.attr('selected','selected');
    }
});

$.inArray() returns the index of the value if it is found, or -1 if it is not in the array. That's why you need to test for >= 0.

View example here-

http://jsfiddle.net/PJs37/

patrick dw