pufAmuf
pufAmuf

Reputation: 7805

Changing option in dropdown via Javascript issue

I'm trying to change the options in a dropdown menu via a script - however nothing seems to happen. No errors either.

Here is the script:

javascript:
var objDropDownMenuName = document.getElementsByName("jjoprs")[0];
function writeText(form) {
    $(objDropDownMenuName.options[1]).selected = true;
    $(objDropDownMenuName).change();
}
writeText(this.form);

Here is the html of the form:

<select name='jjoprs' class='select2'>
                          <option value='NULL' selected> </option>
                          <option value='1060'>Sofi, Laco</option>
<option value='5160'>Vandrlka, Edo</option>
                             </select>

Thanks!

Edit: I am executing this script in IE8

Upvotes: 0

Views: 62

Answers (2)

Martijn
Martijn

Reputation: 16123

function writeText(form) {
    objDropDownMenuName.options[1].selected = true;
    $(objDropDownMenuName).change();
}

The jQuery selector isnt needed to set an option selected


Since you have jQuery:

 function writeText(form) {
     $('select[name="jjoprs"]')
         .find('options:nth-child(2)').attr('selected',true)
         .closest('form').submit();
  }

Upvotes: 1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324790

Try using the correct method:

var sel = document.getElementsByName('jjoprs')[0];
sel.selectedIndex = 1;

Upvotes: 1

Related Questions