13

Can I use a javascript if/else function to change the select options of a form? I don't want to use CSS to hide/display different dropdown menus, so here is what I've ccome up with:

function getType() {
  var x = document.getElementById("food").value;
  var items;

  if (x === "fruit") {
    items = "Apple" || items = "Oranges" || items = "Bananas";
  else {
    items = "Eggplants" || items = "Olives"
  }
  document.getElementById("pickone").value;
}
 
<input type="text" id="food">

 <select id="pickone">
   <option id="1"></option>
   <option id="2"></option>
 </select>

I can't seem to find any documentation about how to do this, so any help would be great.

5
  • 3
    items = "Apple" || items = "Oranges" || items = "Bananas"; what do you want to achieve here? Commented Mar 5, 2018 at 16:03
  • i think i got it - you want to change the select options depending on what you typed into the input field Commented Mar 5, 2018 at 16:07
  • This may help: w3schools.com/jsref/met_select_add.asp Commented Mar 5, 2018 at 16:14
  • 1
    the double pipe symbol || represents an OR statement in JavaScript, it does not delaminate a list of items. Commented Mar 5, 2018 at 16:14
  • Actual proper SelectElement Add documentation. Commented Mar 5, 2018 at 16:30

3 Answers 3

15

You could append a string for the options and set it as innerHTML of your select field afterwards:

function getType() {
  var x = document.getElementById("food").value;
  var items;
  if (x === "fruit") {
    items = ["Apple", "Oranges", "Bananas"];
  } else {
    items = ["Eggplants", "Olives"]
  }
  var str = ""
  for (var item of items) {
    str += "<option>" + item + "</option>"
  }
  document.getElementById("pickone").innerHTML = str;
}
document.getElementById("btn").addEventListener("click", getType)
<input type="text" id="food">
<button id="btn">click</button>
<select id="pickone">
</select>

Sign up to request clarification or add additional context in comments.

4 Comments

This is great - exactly what I was hoping for. When I copy the code though, it doesn't seem to work. Is there a script library it is dependant on?
To clarify, I have no problems with the html portion, but the clicks eems to do nothing when I'm testing it in notepad
@PhysicsIsRelativelyCool no, there is no library needed. If you paste the HTML code and the js this should work. If not you've missed something
Yep, you're right! Your code works perfectly, I had to load the script after the select element, rather than in the <head>
11

Your logic is not very right, specially where you try to do this
items = "Apple" || items = "Oranges" || items = "Bananas";

with the above statement you are saying that itens are Apple OR Oranges OR Bananas, only one at once... you'll need an array of elements, like this:
var itens = ["Apple", "Oranges", "Bananas"];

Then, you will need to loop through it to add them to the select, like this:

var itens = ["Apple", "Orange", "Banana"];
var selectElem = document.getElementById("mySelect");

for (var i = 0; i < itens.length; i++){
  var item = itens[i];
  var element = document.createElement("option");
  element.innerText = item;
  selectElem.append(element);
}
<select id="mySelect"></select>


With that, now you can achieve what you want, just follow this logic...
you can also, if you want, add an `if` statement to check what is the input value, then set the options based on the input value, as you are already trying to do.

1 Comment

@messerbill OP asks how to dynamically add items to a select. This answer showcases that very clearly. OP can hook that logic up their own code. Answer do not need to contain copy paste solutions. If anything they never should.
4

You can change options easily with JavaScript. I would advise to use an additional library to ease DOM manipulation, such as JQuery. An example code would look like the example below. You have to make sure to define an event on which the options should be changed. The example listens to changes within the input field.

<input type="text" id="food" value="vegetables"/>
<select id="pickone"></select>


<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>

<script>
var fruits = ["Apple", "Oranges", "Bananas"];
var vegetables = ["Eggplants", "Olives"];

vegetables.forEach(function(item){
    $('#pickone').append("<option>" + item + "</option>");
});

$('body').on('keyup', '#food', function (){
    if ($('#food').val() === 'fruits') {
        $('#pickone').html("");
        fruits.forEach(function(item){
            $('#pickone').append("<option>" + item + "</option>");
        });
    }
});

</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.