Change jQuery select option programmatically - Doesn't work -


i' m trying remember descision of selected option within jquery select. unfortunatly, can't select wanted option programmatically.

here more details: html:

<div style="width: 140px"> <label for="select-choice-a" class="select">lagerort: </label> <select id="sel_stockids"> <option disabled="disabled">lagerort</option> </select> </div> 

the js (all valid options inserted after ajax-call).:

for (var j = 0; j < paramsparsed.length; j++) {    // paramsparsed {0001, 0002, 0003 etc.}     loadedstocks[j] = '<option value=' + (j+1) + '>' +  paramsparsed[j] + '</option>';     $("#sel_stockids").append(loadedstocks[j]); }     

also listener set select, storing users decision js:

$('#sel_stockids').change(function() {    sessionstorage.setitem("stockid", $('#sel_stockids option:selected').text()); }); 

the "look":

enter image description here

after user have choosen valid "lagerort"-option (stockid), next html-site opened. decision (option text , option value) of user saved in localstorage. when returning page, decision made should still selected option. example, user selects lagerort "0001", changes site , comes - selected option should still "0001". therefor, load stocks again (this works fine)

for (var j = 0; j < paramsparsed.length; j++) {    // paramsparsed {0001, 0002, 0003 etc.}     loadedstocks[j] = '<option value=' + (j+1) + '>' +  paramsparsed[j] + '</option>';     $("#sel_stockids").append(loadedstocks[j]); } 

now want set selected option again. doesn't work.... i've tried many different ways , lost around 2 days (i guess....) simple problem. here approaches far (nothing has worked, yet).... better testing, wanted option hard coded (value = 1, text = 0001 = both link first option within select).

// function called, when user re-opens html-site 1 function reinsertdata() {     // setting first option selected     // $('#sel_stockids option')[1].selected = true;     // $('#sel_stockids option[value=1]')[0].selected = true;     // $('#sel_stockids :nth-child(1)').attr('selected', 'selected');     // $('#sel_stockids option').eq(1).attr('selected', 'selected');     // $("#sel_stockids").val("1");     // $("#sel_stockids").text("0001").attr('selected',true);     // $("#sel_stockids").text("0001").attr('selected','selected');     // $("#sel_stockids").text( "0001" ).prop('selected',true);     // $("#sel_stockids").text( "1" ).prop('selected', selected);     // $("select option").each(function(){     //  if ($(this).text() == "0001")     // 1 finds right option...... doesn't select     //    $(this).attr("selected","selected");     // });   } 

inserting of approaches within jsfiddle works - none of shown code helps within app. js-versions i'm using jquery-mobile-1.3.0 , jquery-1.9.1.min. very grateful or hints!

$("#sel_stockids").val("1"); 

is right way it.

also, may want wrap value attribute value in quotes:

loadedstocks[j] = '<option value="' + (j+1) + '">' +  paramsparsed[j] + '</option>'; 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -