• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
You are here: Home / jQuery / Using jQuery to Set Select to the First Option

Using jQuery to Set Select to the First Option

July 25, 2022 Leave a Comment

We can use jQuery to set select to the first option by using the jQuery val() method to set the value of the select element to the value of the first option.

$("#select-element").val($("#select-element option:first").val());

If using WordPress, don’t forget to change the $ to jQuery:

jQuery("#select-element").val(jQuery("#select-element option:first").val());

Let’s see an example of this below.


Lets say we have the following HTML:

<div id="div1">
  <select id="select1">
    <option value="car">Car</option>
    <option value="bus" selected>Bus</option>
    <option value="train">Train</option>
  </select>
  <div id="click-me" onclick="changeSelect()">Change select to first option</div>
</div>

In this example, the selected option to start will be the middle option, Bus. We can put our code above to use to make it so that when the user clicks the button, the selected option will be changed to the first option, Car.

We will create a function that runs this code when the user clicks our button.

Here is the JavaScript code we will need:

function changeSelect(){
  $("#select1").val($("#select1 option:first").val());
}; 

Try it out below:

Code Output:

Change select to first option

Full Code:

<div id="div1">
  <select id="select1">
    <option value="car">Car</option>
    <option value="bus" slected>Bus</option>
    <option value="train">Train</option>
  </select>
  <div id="click-me" onclick="changeSelect()">Change select to first option</div>
</div>

<script>

function changeSelect(){
  $("#select1").val($("#select1 option:first").val());
}; 

</script>

Using jQuery to Set Select to the Last Option

To set the Select to the last option, we can use our code from above, just change first to last.

$("#select-element").val($("#select-element option:last").val());

Let’s see this in action below:

Code Output:

Change select to last option

Full Code:

<div id="div1">
  <select id="select2">
    <option value="car">Car</option>
    <option value="bus">Bus</option>
    <option value="train">Train</option>
  </select>
  <div id="click-me" onclick="changeSelect2()">Change select to last option</div>
</div>

<script>

function changeSelect2(){
  $("#select2").val($("#select2 option:last").val());
}; 

</script>

Using jQuery to Set Select to a Specific Option

To set the Select to a specfic option, in this case the middle option, Bus, we can use the nth-child() selector.

$("#select-element").val($("#select-element option:nth-child(2)").val());

Let’s see this in action below:

Code Output:

Change select to middle option

Full Code:

<div id="div1">
  <select id="select3">
    <option value="car">Car</option>
    <option value="bus">Bus</option>
    <option value="train">Train</option>
  </select>
  <div id="click-me" onclick="changeSelect3()">Change select to middle option</div>
</div>

<script>

function changeSelect3(){
  $("#select3").val($("#select3 option:nth-child(2)").val());
}; 

</script>

Hopefully this article has been useful to help you understand how to use jQuery to set select to the first option.

Other Articles You'll Also Like:

  • 1.  Using jQuery to Get the Bottom Position of Element
  • 2.  Using jQuery to Disable Input
  • 3.  Using jQuery to Add Required Attribute to Input Field
  • 4.  How to Use jQuery to Remove Element
  • 5.  Using jQuery to Check if Checkbox is Checked
  • 6.  Sort List of Divs with Data Attribute with Radio Buttons Using Javascript
  • 7.  Using jQuery to Get the Margin of an Element
  • 8.  jQuery slideDown – How to Slide Down and Show an Element Using jQuery
  • 9.  Using jQuery to Get the Parent Div of an Element
  • 10.  How to Filter a List of Divs with a Text Input Bar Using Javascript

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy