0

I have a SELECT in a FORM. This select is populated using js.

I need to add a "Selected" attribute to one of these options. I get which one, by checking a MySql database to see the name of the community which needs to have a "selected attribute" added to it.

<select name="community" id="community">
    //OPTIONS HERE
</select>

The filler() function:

function filler(com){

//com is the options which needs to be selected, this variables value comes from the mysql database

var community = document.getElementById("community");
var area = document.getElementById("area").value;
// area is just another input on the page which value also is fetched from mysql db. Each area has x communities, so I have alot of IF:s.


if(area == 'Blekinge') {
community.length = 6;
community.options[0].value = "Välj Kommun";
community.options[0].text = "-- Välj Kommun --";
community.options[0].id = "Välj Kommun";
community.options[1].value = "Karlshamn";
community.options[1].text = "Karlshamn";
community.options[1].id = "Karlshamn";
community.options[2].value = "Karlskrona";
community.options[2].text = "Karlskrona";
community.options[2].id = "Karlskrona";
community.selected = 0;
}

   }

As you can see, "com" variable is the option which needs to have the "selected" attribute added to it.

I have over 30 of these if-statements, and I have no clue how to create a function to add this "Selected" attribute to the matching option.

So I have "com" which for example could be "Karlskrona" in the example above. How should I add the selected to it?

I need a simple function for this which works in all major browsers...

2
  • Just an observation, do your <option> elements need IDs? Commented Nov 8, 2010 at 13:47
  • @Nick: Yes they do, thats how I "remember" them when page is reloaded. Commented Nov 8, 2010 at 13:48

4 Answers 4

1

Set the selectedIndex property of the SELECT to whichever index you need. Zero-based, of course.

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

2 Comments

I have as I said 30 more IF statements, where the index would change 30 times.
As well you could add through jquery.
1

Just do

community.value = com;

example at http://www.jsfiddle.net/jMapA/

2 Comments

The select must be populated with the communities as well, so the user can change this if pleased to.
@Camran, at the end of your code do this.. it does not alter the contents of the select, it just selects the option that has the value of com
0
for(var i = 0; i < community.options.length; i++) {
  if(community.options[i].id == com)
    community.selectedIndex = i;
}

Comments

0
function selectOptionValue(selectId, value)
{
    select = document.getElementById(selectId);
    if (select)
    {
        for (var i = 0; i < select.options.length)
        {
            if (select.options[i].value == value)
            {
                select.options[i].selected = 'selected';
                return true;
            }
        }
    }

    return false;
}

Use my function like this:

selectOptionValue('community', 'Karlskrona');

Comments

Your Answer

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