1

I get a string like from server using jquery:

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

then I create a dropdown within a div:

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

problem is that when I create this, I want to select one option so:

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

but doesn't work! if I add an alert before calling this line, then this works!

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

can not find what is problem.

NOTE: I can not change the way I get data from server.

7
  • Well the edited question code works as expected for me. Which browser are you seeing the problem in? Commented Jul 25, 2013 at 16:23
  • 3
    I assume the line $("#mysel").val("3"); is outside of the $.get() {}? The response from $.get is asynchronous so the $("#mysel").val("3"); executes before the HTML <option> elements are added. Adding the alert() delays the execution long enough for the <option> to exist in that scenario. Solution: move $("#mysel").val("3"); inside the $.get() {} body. Commented Jul 25, 2013 at 16:26
  • @andyb: Probably. He should just post all the relevant code together. But I think that's exactly what is causing this. Commented Jul 25, 2013 at 16:32
  • @andyb yes! that is it, you are correct :D Commented Jul 25, 2013 at 16:35
  • Added my comment as an answer. I'll leave it up to you whether you want to just delete the whole question or accept an answer. Commented Jul 25, 2013 at 17:12

7 Answers 7

2

You are not providing the right id. Your select's id is mysel and you are trying to select an option in mydiv. Change

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

to

$("#mysel").val("3");
Sign up to request clarification or add additional context in comments.

2 Comments

try this setTimeout('$("#mysel").val("3")', 1);
Should avoid trying to hack something using a timeout, you rely on something happening within an arbitrary time and the user can see an item changing after being appended to the dom. This is what callbacks are for.
1

You need to use get()'s callback. You can use the success one you already have:

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

or you can use done:

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

Edit: actually, rather than adding something to the dom and then modifying it, it would be better to modify before inserting.

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

Comments

0

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

The response from $.get is asynchronous so the $("#mysel").val("3"); executes before the HTML <option> elements are added.

Adding the alert() delays the execution long enough for the <option> to exist in that scenario.

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

Comments

0

You need to set the selectedIndex property:

document.getElementById("mySel").selectedIndex = 3;

3 Comments

I have not access to data that provide index values.
Then: document.getElementById('mySel').value = 3;
Other than a slight boost in performance, document.getElementById('mySel').value = 3; wouldn't do anything different than $("#mysel").val("3");
0

Try:

$("#mysel option:selected").val("3");

And if you want to set the text try:

$( "#mysel option:selected" ).text("3");

But you must declare this after your dropdown is created in the DOM tree.

Comments

0
$(document).ready(function(e) {
    $.get('server.php',{dataType:'text/html'}, function(data) {
         $('#mydiv').html('<select id="mysel" name="mysel">'+data+'</select>');
         $("#mysel").val("3");
    })
});

try the above code;

Comments

0

Try this

$(document).ready(function(){
   $("select#mysel option").each(function() { 
       if($(this).text() == '2')
          this.selected = true;
   });
});

1 Comment

The problem is that mysel will not be there on ready(). Also, there is no need to add select qualifiers on top of an id as id's are unique. Use #mysel rather than select#mysel.

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.