Set selected option of select box

I want to set a option that was selected previously to be displayed on page load. I tried it with the following code: $(“#gate”).val(‘Gateway 2’); with <select id=”gate”> <option value=”null”>- choose -</option> <option value=”gateway_1″>Gateway 1</option> <option value=”gateway_2″>Gateway 2</option> </select> But this does not work. Any ideas? 16 Answers 16

Working with select using AngularJS’s ng-options

I have read about it in other posts, but I couldn’t figure it out. I have an array, $scope.items = [ {ID: ‘000001’, Title: ‘Chicago’}, {ID: ‘000002’, Title: ‘New York’}, {ID: ‘000003’, Title: ‘Washington’}, ]; I want to render it as: <select> <option value=”000001″>Chicago</option> <option value=”000002″>New York</option> <option value=”000003″>Washington</option> </select> And also I want to … Read more

How can I get new selection in “select” in Angular 2?

I am using Angular 2 (TypeScript). I want to do something with the new selection, but what I get in onChange() is always the last selection. How can I get the new selection? <select [(ngModel)]=”selectedDevice” (change)=”onChange($event)”> <option *ngFor=”#i of devices”>{{i}}</option> </select> onChange($event) { console.log(this.selectedDevice); // I want to do something here with the new selectedDevice, … Read more

Change the selected value of a drop-down list with jQuery

I have a drop-down list with known values. What I’m trying to do is set the drop down list to a particular value that I know exists using jQuery. Using regular JavaScript, I would do something like: ddl = document.getElementById(“ID of element goes here”); ddl.value = 2; // 2 being the value I want to … Read more

Set select option ‘selected’, by value

I have a select field with some options in it. Now I need to select one of those options with jQuery. But how can I do that when I only know the value of the option that must be selected? I have the following HTML: <div class=”id_100″> <select> <option value=”val1″>Val 1</option> <option value=”val2″>Val 2</option> <option … Read more

How do you remove all the options of a select box and then add one option and select it with jQuery?

Using core jQuery, how do you remove all the options of a select box, then add one option and select it? My select box is the following. <Select id=”mySelect” size=”9″> </Select> EDIT: The following code was helpful with chaining. However, (in Internet Explorer) .val(‘whatever’) did not select the option that was added. (I did use the … Read more

jQuery get specific option tag text

All right, say I have this: <select id=’list’> <option value=”1″>Option A</option> <option value=”2″>Option B</option> <option value=”3″>Option C</option> </select> What would the selector look like if I wanted to get “Option B” when I have the value ‘2’? Please note that this is not asking how to get the selected text value, but just any one … Read more

What is the best way to add options to a select from a JavaScript object with jQuery?

What is the best method for adding options to a <select> from a JavaScript object using jQuery? I’m looking for something that I don’t need a plugin to do, but I would also be interested in the plugins that are out there. This is what I did: selectValues = { “1”: “test 1”, “2”: “test … Read more