select an option in already populated select tag using JS

javascript populate select options from array
javascript select option
javascript add option to dropdown
javascript add option to select
javascript loop through select options
javascript clear select options
javascript new option
html <select

Suppose I have a select tag as following:

<select name='selectBox' id='selectBox'>
    <option value='1'>one</option>
    <option value='2'>two</option>
    <option value='3'>three</option>
    <option value='4'>four</option>
    <option value='5'>five</option>
</select>

and I have a value in a variable, for example: selectVal = 'three' and I want to select the option based on text not on value.

I looked on

$('#selectBox').prop('selectedIndex', 3);
$('#selectBox').selectmenu('refresh');

But I want to select on text of option. I'm beginner in this stuff, any help will be highly appreciated!

Thanks.

try this:-

var text = "five"
$(document).ready(function() {
  $('#selectBox option').each(function(index){
    if($(this).text() === text){
        $('#selectBox').prop('selectedIndex', index);
    }
  })
});

Add options to an HTML select box with Javascript, Friday's post will look at how to add a language selection drop down box in FCKEditor, using the way of populating a select drop down box with Javascript from an  Also Read: How to Use AngularJS ng-options to Bind or Populate JSON Array to a SELECT DropDownList. Extract Data from an External JSON File and Bind Data to <select> Element. The markup for this example remains the same as above.

You have to get the options index:

 var options = $("#selectBox > option");
 $('#selectBox').prop('selectedIndex', options.index(":contains(three)"));

HTML DOM Select options Collection, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  This is a JavaScript tutorial on how to populate a SELECT element’s options with an Ajax request. In this tutorial, I will be using the JQuery library. In this example, I will be sending an Ajax request to a PHP script, which will return a list of car models in a JSON format.

You can try the following way by using each():

let selectVal = 'three';
$('#selectBox option').each(function(){ 
  if($(this).text() == selectVal)
    $('#selectBox').prop('selectedIndex', 2);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name='selectBox' id='selectBox'>
    <option value='1'>one</option>
    <option value='2'>two</option>
    <option value='3'>three</option>
    <option value='4'>four</option>
    <option value='5'>five</option>
</select>

How to Populate Select Options using Javascript, In this video tutorial, you will learn how to populate select options using javascript from array Duration: 4:31 Posted: Nov 4, 2019 Line 1 targets the select element. Line 2 clears any options in the element. Lines 4-5 appends our default option. Line 10 defines the URL where we can find our JSON data. Lines 12-13 initializes the remote request. Lines 19-24 creates an option element for each entry found and adds it to the select list.

This is pure vanilla js

var element = document.getElementById(id);
element.value = valueToSelect;

Programmatically Preselect Dropdown Using Javascript, So if we wish to pre-select item 3 for the above example, we can use <select name="ddl_example2"> <option value="1">item1</option> <option value="  You have a select element, and you need to “select” one of its options based on one of its values. What you do is use the “selected-selector” of jQuery to do it in a single line. Lets say I have the following select element and I need to dynamically select the option with a value of 3, which would be the “Peach”.

Dynamic Select Box: Replacing Options Based on Selection, The associated select box can be populated with option elements using JavaScript as the page loads. The following HTML markup generates the above form:. In fact using <select> tag you can create following types of HTML list controls. Dropdown List (By Default) List Box (By adding size attribute) The following code creates two list box named 'firstList' and 'secondList'. At this point I have not specified any value to be displayed on lists because I’ll be using JavaScript to populate them.

jQuery: Remove all the options of a select box and then add one , jQuery Practical Exercises with Solution: Remove all the options of a select box and then add one option and select it with jQuery. Dynamically generated select option dropdown menu using JavaScript Step 1: We will get all necessary data of both dropdown listing on page load and stored into variable. Step 2: We will convert that array into json object.You need to put below code into head section of index.html file.

JavaScript – Get selected value from dropdown list – Mkyong.com, A JavaScript example to show you how to get the selected value or text from a dropdown list. A drop box list. <select id="country"> <option value="None">-- Select --</option> <option value="ID001">China</option> <option How do I populate the value option value from mysql db using a Php array. The selectedIndex property sets or returns the index of the selected option in a drop-down list. The index starts at 0. Note: If the drop-down list allows multiple selections it will only return the index of the first option selected. Note: The value "-1" will deselect all options (if any). Note: If no option is selected, the selectedIndex

Comments
  • Possible duplicate of How to select an option by its text?
  • Possible duplicate of How to populate the options of a select element in javascript