1

How can i set multiple values for multiple select. This is what i have tried so far in jquery. I looking for a jquery as well as pure js solution(just for my knowledge).

for(x in gList)
{
    $('#GList').val(gList[x]);
}

This is selecting only one and not all.

EDIT: I found another solution in jquery . Hope this too helps others. In the loop use

$("#GList option[value=" + gList[x] +"]").attr("selected","selected") ;
3
  • 1
    This is in the docs. api.jquery.com/val at Example: Set a single select, a multiple select, checkboxes and a radio button .. Commented Sep 21, 2011 at 7:47
  • Isn't your alternative solution a little too awkward and cumbersome? I'd suggest following docs guidelines Commented Sep 21, 2011 at 7:58
  • yes i know that's why i am asking Commented Sep 21, 2011 at 8:01

1 Answer 1

3

Try:

$('#GList').val(gList);

As per: http://api.jquery.com/val/#val2

as a side note, with:

for(x in gList){
    $('#GList').val(gList[x]);
}

You select the 1st item in gList then you select the 2nd, deselecting the 1st, and so on...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.