Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
<select class="select form-control" id="ifacility" name="facility">
    <option value="" selected="selected">------</option>
    <option value="1">Room 1</option>
    <option value="2">Room 1</option>
    <option value="3">Room 2</option>
    <option value="4">Room 3</option>
</select>
</body>
</html>
 
function setOptionByValue(select, value){
    var options = select.options;
    for(var i = 0, len = options.length; i < len; i++){
        if(options[i].textContent === value){
            select.selectedIndex = i;
            return true; //Return so it breaks the loop and also lets you know if the function found an option by that value
        }
    }
    return false; //Just to let you know it didn't find any option with that value.
}
setOptionByValue(document.getElementById('ifacility'), 'Room 2');
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
AmaanCpro
0viewers