Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery change value of select list

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

like image 860
sipher_z Avatar asked Apr 05 '12 16:04

sipher_z


People also ask

How can I change the selected value of a DropDownList using jQuery?

Using the jQuery change() method; you can set the selected value of dropdown in jquery by using id, name, class, and tag with selected html elements; see the following example for that: Example 1 :- Set selected value of dropdown in jquery by id.

How do I set the value of a element in jQuery?

jQuery val() Method The val() method returns or sets the value attribute of the selected elements. When used to return value: This method returns the value of the value attribute of the FIRST matched element.

How do you display a selected value in a drop down list?

Method 1: Using the value property: The value of the selected element can be found by using the value property on the selected element that defines the list. This property returns a string representing the value attribute of the <option> element in the list. If no option is selected then nothing will be returned.

How can set the selected value of dropdown in HTML?

The select tag in HTML is used to create a dropdown list of options that can be selected. The option tag contains the value that would be used when selected. The default value of the select element can be set by using the 'selected' attribute on the required option. This is a boolean attribute.


2 Answers

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/

like image 174
PeeHaa Avatar answered Sep 25 '22 13:09

PeeHaa


$('#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.

like image 39
Kris Krause Avatar answered Sep 25 '22 13:09

Kris Krause