1

I have created a simple dropdown list which is populated from a previous dropdown list.

How do i make all selected?

Mentioned below are the codes for the javascript populating it.

function OnClkAddButtonServer(form)
{
    var selObj = document.getElementById('List1');
     var selObj2 = document.getElementById('List2');

      var i;
      var count = selObj2.options.length;

      for (i=0;i<selObj.options.length;i++) 
      {
         if (selObj.options[i].selected) 
         {
               var option = new Option(selObj.options[i].text,selObj.options[i].value);
               option.title = selObj.options[i].text;
               selObj2.options[count] = option;
               count=count+1;
               selObj.options[i] = null;
               i--;
         }
      }
}

Thanks in advance

2
  • 1
    Maybe you should consider revising your question. Does PHP have anything to do with your problem? Seems like all you want to do is select all of the items in a list which is an HTML issue. Commented Jul 3, 2012 at 18:01
  • Did you try var option = new Option(...); option.selected = true;? Commented Jul 4, 2012 at 7:47

2 Answers 2

1

If you are attempting to have all options in a list be selected, you need the HTML to look like this.

<select multiple="multiple">
  <option selected="selected">Volvo</option>
  <option selected="selected">Saab</option>
  <option selected="selected">Mercedes</option>
  <option selected="selected">Audi</option>
</select>

To make a newly created option be selected you can use the Javascript method of altering HTML attributes like so.

document.getElementById("idElement").setAttribute("class", "className");

This example requires you to know the ID of the element but if you have reference to the element in another fashion you can skip the wasteful lookup.

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

3 Comments

Hi, i understand that in html you can just use selected="selected". But the problem is that my dropdown list is empty. It is being populated by the javascript button mentioned on top. I am unable to submit my form because the newly populated dropdown list has nothing selected.
I have a button which calls the javascript on top to populate this new dropdown list.
Updated my answer to include an example of changing attributes.
0

First of all, if you want to have all of the dropdown options selected, you need to make sure the select type is multiple.

For example, assuming selObj2 refers to a <select> tag (<select id = "List2">), you would want to add the "multiple" attribute to the select tag:

<select id = "List2" multiple = "multiple">

If you want to do it in the JS, you can insert the line:

selObj2.setAttribute("multiple", "multiple");

or,

selObj2.multiple = true;

This is of course, assuming that selObj2 is a <select> tag. However, you must enable the multiple selects for multiple options to be selected. Once you have enabled this attribute, you can go onto the next step...

Now, assuming that selObj2.options refers to individual options that you'd like to have selected, you can do this in the JS (just add this block right before the ending brace for the function OnClkAddButtonServer):

for (var i=0; i<selObj2.options.length; i++)
{
    selObj2.options[i].setAttribute("selected", true);
    // or,
    // selObj2.options[i].selected = true;
}

Also, notice in the for loop that I used:

var i=0;

This is because we are creating the variable i local to the scope of the loop, meaning that if we want to use i outside of the loop, the two instances won't conflict. If you didn't use the keyword "var" then the variable i will be accessible anywhere in the whole script (created a global variable). It's a good habit to keep variables local, and since people tend to use i or x as counters in these loops often, it's a good bit of precaution to take in order to avoid any sort of nasty surprises.

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.