All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

JavaScript Set Selected Option By Value

Last Updated : Mar 11, 2024

JavaScript Set Selected Option By Value

In this tutorial we will show you the solution of JavaScript set selected option by value, as there are many ways with help of which we can set selected options by value with the help of JavaScript.

We can also create a custom function with help of which we can set selected options by value using JavaScript.

So, let us see the example given below with help of which we can set the selected option by value.

Step By Step Guide On JavaScript Set Selected Option By Value :-

As to get a set value in the selected option we have to first create a select and then have to create some option inside this with some values.

Now, at last how to create a button or something. On trigger or say click on which we have to set values. So, with the assumption given let us see the codes and method which we are going to use.

 JavaScript set selected option by value
<label for="select">Choose your Favourite Car:</label>
    <select id="select">
        <option value="" selected disabled hidden>--------------</option>
        <option value="mercedes">Mercedes</option>
        <option value="bmw">BMW</option>
        <option value="audi">Audi</option>
        <option value="rr">Rolls Royce</option>
        <option value="tesla">Tesla</option>
    <button type="button" onclick="result('audi')">
    <button type="button" onclick="result('mercedes')">
    <button type="button" onclick="result('bmw')">
    <button type="button" onclick="result('rr')">
        Rolls Royce
    <button type="button" onclick="result('tesla')">
      function result(valueToSelect) {
          document.getElementById('select').value = valueToSelect
  1. As we here see that we today write only JavaScript codes in our example.
  2. In previous tutorials, we already many times create a full code example in which we first create an HTML form with its basic structure. We use HTML, head, title, body, and script tags inside that structure.
  3. As in the example given above we see that we create some heading tags. After that, we create a label and then select a tag. Inside the select tag, we create some options. You can see the options that we created above.
  4. Now, after that we create some buttons. The number of buttons is equal to the number of options available in the select tag. Now, our today’s task is to set the selected option value using JavaScript. So, the ways with help of which we want to set the value inside select may be different but the method remains the same. So, as we see above that a function gets called when we click on any button.
  5. And we also send a parameter to it. Now, inside the function, we just set the value of the select option based on the values of options. So, when you click on the button parameter gets passed. When the parameter matched with the value of the option the current option gets shown or activated.

Conclusion :-

At last, in conclusion, here we can say that with the help of this article we can understand how to set selected options by values using JavaScript.

I hope this tutorial on JavaScript set selected option by value helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪