0

I have the following select:

<select id="select_produckt" class="form-control">
    <option id="select_default" selected="" value="0">Vælg produkt</option>
    <option value="1">NP 89,-</option>
    <option value="2">NN 89,-</option>
    <option value="3">NP 99,-</option>
    <option value="4">NN 99,-</option>
    <option value="5">NP 119,-</option>
    <option value="6">NN 119,-</option>
    <option value="7">NP 139,-</option>
    <option value="8">NN 139,-</option>
    <option value="9">NP 169,-</option>
    <option value="10">NN 169,-</option>
    <option value="11">NP 239,-</option>
    <option value="12">NN 239,-</option>
</select>

Now, I wish to simply set the value of the select with JavaScript (meaning that the options text will be shown).

I've tried the following:

$('#select_produckt').val(2)

This, however, just left the field blank.

Can anyone tell me what the problem is?

Update

    $.ajax({
    type: 'POST',
    url: '/Excel_Data/getSale',
    data: {
        request: 'ajax',
        row_id: id,
        list_id: list_id
    },
    success: function (data) {
        $('#product_selection').removeClass('hidden');
        alert(parseInt(data))
        $('#select_produckt').val(""+data)
    }
});
5
  • 4
    Works fine.... jsfiddle.net/S4n3v Commented Feb 14, 2014 at 0:07
  • Make sure your code is in the $(document).ready() function. Commented Feb 14, 2014 at 0:08
  • Your JavaScript maybe has other errors preventing it from executing? The code you've shown works as @Anthony points out. Commented Feb 14, 2014 at 0:08
  • @AnthonyChu ive updated my code to match (the ajax call im making returns "2") but apperntly i keep getting nan Commented Feb 14, 2014 at 0:13
  • fixed it! apprently it was because it was an array :P Commented Feb 14, 2014 at 0:15

1 Answer 1

1

Try this:

$('#select_produckt').find('option')[2].selected = true;

http://jsfiddle.net/qwJKV/

Sign up to request clarification or add additional context in comments.

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.