0

I am trying to use javascript on the form's load to pre-select some options. This is what I have.

    var mar=document.getElementById('region');
    var marketarray = '@marketlistdb@'.split("|");
    for( var l = 0; l < mar.options.length; l++ )
    {
        var o = mar.options[l];
        if ( marketarray.indexOf( o.text ) != -1 )
        {
            mar.selected = true;
        }
    }

But this will not select the elements in the select tag. I saw this solution elsewhere (Javascript to Select Multiple options) I tried this solution, but it will not work for whatever reason. Does anyone see anything wrong that would cause this not to select?

We have placed alerts within the for loop and within the if statement to verify that we have true matches between the array and each individual option.

12
  • P.S this is written in the $document.ready section. Commented Oct 3, 2014 at 21:32
  • 2
    var marketarray = '@marketlistdb@'.split("|"); - what? Commented Oct 3, 2014 at 21:32
  • It creates an array comprised of different markets resulting from a SQL query Commented Oct 3, 2014 at 21:33
  • If you put console.log(marketarray) right after that line, what is the result? Commented Oct 3, 2014 at 21:34
  • 2
    I believe you want mar.options[l].selected = true Commented Oct 3, 2014 at 21:41

0

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.