Skip to content
Fix Code Error

How do I programmatically set the value of a select box element using JavaScript?

March 13, 2021 by Code Error
Posted By: brass-kazoo

I have the following HTML <select> element:

<select id="leaveCode" name="leaveCode">
  <option value="10">Annual Leave</option>
  <option value="11">Medical Leave</option>
  <option value="14">Long Service</option>
  <option value="17">Leave Without Pay</option>
</select>

Using a JavaScript function with the leaveCode number as a parameter, how do I select the appropriate option in the list?

Solution

You can use this function:

selectElement('leaveCode', '11')

function selectElement(id, valueToSelect) {    
    let element = document.getElementById(id);
    element.value = valueToSelect;
}
Answered By: Mitchel Sellers

Related Articles

  • How do i update a javascript variable as its value changes?
  • center 3 items on 2 lines
  • How to filter an Observable array in angular based…
  • Having trouble with my nav bar/header, It used to…
  • Javascript validate all checkboxes are selected
  • Why doesnt my table sort my div variable in numerical order?
  • How to print and sort list of sellers based on Bonus…
  • jest.fn() claims not to have been called, but has
  • how to use canvas in JavaScript flappy bird code
  • collision detection from picturebox in a list c#
  • I made a game it should display two images when i…
  • ggplot by group with filter()
  • How to create a game over screen for a basic HTML/JS game?
  • InfluxDB - ERR_EMPTY_RESPONSE - Used to come up in…
  • Navbar not filling width of page when reduced to mobile view
  • Avoid using document.getElementById with Backbone.js
  • Getting a "TypeError" when trying to validate a form
  • How can I require at least one checkbox be checked…
  • How do I switch two players in tic-tac-toe game in Python?
  • Disabling a button upon condition in Google App script
  • Loop through multiple files in a folder a copy and…
  • long long int vs. long int vs. int64_t in C++
  • How do I keep only the first map and when the game…
  • What's the Android ADB shell "dumpsys" tool and what…
  • Generated row of table event listener not working instantly
  • javascript minesweeper issue with revealing tiles…
  • Js Calculation based on radio button, and some problems
  • Using setInterval for creating animation
  • Flappy bird code not working - JavaScript
  • Displaying the Dates that user have chosen from date input
  • React useState hook causes infinite rendering
  • Setting DIV width and height in JavaScript
  • setTimeout function not working : javascript
  • return two dimensional array from function with…
  • What is the difference between "long", "long long",…
  • Peak signal detection in realtime timeseries data
  • Active tab issue on page load HTML
  • CSS Input with width: 100% goes outside parent's bound
  • AngularJS : Factory and Service?
  • Dropdown using javascript onchange
  • OpenCL - Approximation of Pi via Monte Carlo…
  • Changing an image with a dropdown, only works with…
  • How to update data from vue-tables-2 after action…
  • Why my "title" bloc doesn't go to bottom?
  • How can I center all my contents in html?
  • Neither BindingResult nor plain target object for…
  • JavaScript TypeError: Cannot read property 'style' of null
  • Form field border-radius is not working only on the…
  • How to create a drop shadow only on one side of an element?
  • How to add fade animation for this tab
  • Keycloak/Wildfly How to configure all console logs…
  • Form validation gets bypassed when I'm hitting the…
  • Tailswind css - "list-disc" is not styling bullets…
  • Why is Ember throwing "Uncaught Error: Assertion…
  • ConstraintLayout - center and constraint
  • Simple pagination in javascript
  • Could not install Gradle distribution from…
  • I need help figuring out how to add values
  • Javascript change color of text and background to…
  • Injecting $scope into an angular service function()
  • How to specify 64 bit integers in c
  • C++ Switch statement to assign struct values
  • NextJS: Push new query string value to an existing…
  • Uncaught (in promise) TypeError: states.filter is…
  • Error stopping function after alert message is shown
  • showing record text with red color when click stop button
  • How can I change an element's class with JavaScript?
  • I'm trying to take inputs from an HTML form and pass…
  • Best way to bind textfield to a model in ember
  • HTTP Status 500 - org.apache.jasper.JasperException:…
  • Trouble with boxes appearing/hiding based on selection
  • Polymer 1.0 Trying to make a splitter which works…
  • Making a grading calculator using javascript
  • Start / Stop a Windows Service from a…
  • Use ImmediateRequeueMessageRecoverer in Spring…
  • Which are the differences between vue-cli and…
  • Stop and Start a service via batch or cmd file?
  • Run foreach on parsed JSON nested array
  • JQuery function not working after another row in my…
  • Ember 2, filter relationship models (hasMany,…
  • Restful API service
  • How to select an option from drop down using…
  • Why another instance method call doesn't run?
  • Cannot send array list from JavaScript to Django View
  • Palindromic numbers in Java
  • Change onclick action with a Javascript function
  • How to disable previous cloned element
  • JS file is not updating to new id values
  • Google Forms file upload complete example
  • JavaScript gives NaN error on the page but variable…
  • jQuery Mobile: document ready vs. page events
  • android studio 0.4.2: Gradle project sync failed error
  • Firestore get field value with document ref
  • emberjs find then filter
  • AngularJS : When to use service instead of factory
  • Plot a horizontal line using matplotlib
  • send nextjs post to nodejs api
  • Stripe Elements card.mount function error after…
  • how can select from drop down menu and call…
  • Dialogflow to Firestore using Inline Fulfillment -…

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Could not reserve enough space for object heap

Next Post:

finding and replacing elements in a list

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error