7

I have the following code

<select id="practice_id" name="practice" column="practice" style="width: 200px">
 <option value="">Default</option>
 <option value="1">Test 1</option>
 <option value="2">Test 2</option>
 <option value="3">Test 3</option>
 ...............
</select>

What I need to do, is to change the value of the selected item to one based on a query string number (I can get this value ok)

I am trying to change the value using jQuery, but nothing I've tried seems to work.

$('#practice_id').val('QUERY_STRING_NUMBER');

This doesn't seem to work as the value doesn't change.

I am I getting something wrong here?

Thanks

2
  • probably you can remove the currently selected option and replace it with an <option value="QUERY_STRING_NUMBER">? Commented Apr 5, 2012 at 16:16
  • @DG3 Or just change the value :-) Commented Apr 5, 2012 at 16:24

2 Answers 2

10

You need to select the option element and not the select element:

(function($) {
    $('#practice_id').change(function() {
        var val = $(this).val();
        alert(val);
        $('option[value=' + val + ']', this).val('changed');
    });
})(jQuery);

http://jsfiddle.net/PeeHaa/6yre7/

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

Comments

1

$('#practice_id').val('QUERY_STRING_NUMBER');

This is setting the value of the dropdown, not "changing" the underlining option value.

$(document).ready(function() { $("#practice_id").children().val("-99"); });

This will change the value to -99 on all of the option tags.

http://jsfiddle.net/MejLX/1/

Note: Yes, you can do this via jQuery. However, based on most everyday common scenarios I would think that your server-side script/page would be generating the correct value.

2 Comments

Is there a way to change option value?
Sorry about the updates as something weird was going on with the editting.

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.