javascript - How select default option of dynamically added dropdown list -


i string server using jquery:

<option value='1'>1</option><option value='2'>2</option> <option value='3'>3</option><option value='4'>4</option> 

then create dropdown within div:

$.get('server.php', function(data) {     $('#mydiv').html('<select id="mysel" name="mysel">'+data+'</select>'); }); 

problem when create this, want select 1 option so:

$("#mysel").val("3"); 

but doesn't work! if add alert before calling line, works!

alert('test'); $("#mysel").val("3"); 

can not find problem.

note: can not change way data server.

assuming line $("#mysel").val("3"); outside of $.get() {}...

the response $.get asynchronous $("#mysel").val("3"); executes before html <option> elements added.

adding alert() delays execution long enough <option> exist in scenario.

the simplest solution here move $("#mysel").val("3"); inside $.get() {} body.


Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

java - More than one row with the given identifier was found: 1, for class: com.model.Diagnosis -