1

I have a dropdown

<select class="form-control" id="defectstatus" name="defectstatus">
   <option selected="selected" value="Pick One">Select Defect Status</option>
   <option value="New">New</option>
   <option value="Needs more info">Needs more info</option>

Now I have a variable

a=$("#answer").html();
alert(a);

For which the value of a is

New

What I want to do is something like this

$("#defectstatus").val(a);

Then my dropdown defectstatus will change default selected to New instead of Pick One

2
  • Nevermind. Didn't the see the output value Commented Dec 16, 2015 at 1:06
  • Whatever you have seems to work fine : jsfiddle.net/DinoMyte/czbv0hag . Whats the issue ? Commented Dec 16, 2015 at 1:10

3 Answers 3

1

As I understand you trying to select the option with the value is (New) you can use

$("#defectstatus > option[value='"+ a +"']").prop('selected' , true);
Sign up to request clarification or add additional context in comments.

2 Comments

This is exactly what I want, but still it doesn't work.The dropdown selected doesn't change
@Chong maybe you need to trim the html() to avoid left and right white spaces .. you can use a=$.trim($("#answer").html());
0

You try change $("#answer").text(); instead of $("#answer").html();

Comments

0

What you have seems like it works, but I imagine it's possible that your code is executing before the HTML has loaded, and is therefore not finding #answer.

You can wrap your code in $(function(){/*code*/}); to make sure it executes when the document is ready.

(Live example: http://plnkr.co/edit/PMOraMzVsMEg2z3FOeP5?p=preview)

Comments

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.