How to Manipulate a Select Options Based on Another Select Option in HTML

How to manipulate a select options based on another select option in html?

You need to associate a change event listener to the select dropdown with name start so that when each option is selected from this dropdown you can show/hide the option in end dropdown like this. Also, make note of parseInt() which will compare the option value as a integer and give you the correct result.

$('select[name="start"]').change(function(){  var selectedValue = parseInt($(this).val());  changeEndOptions(selectedValue);});
function changeEndOptions(startValue){ var $options = $('select[name="end"] option'); $options.each(function(){ var optionVal = parseInt($(this).val()); if(optionVal <= startValue){ $(this).hide(); } else { $(this).show(); } });}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><select class="form-control" name='start'>    <option selected disabled hidden value="">start</option>
<option>1</option> <option>2</option> <option>3</option> <option>4</option> <option>5</option> <option>6</option> <option>7</option> <option>8</option> <option>9</option> <option>10</option>
</select>

<select class="form-control" name='end'> <option selected disabled hidden value="">end</option>
<option>2</option> <option>3</option> <option>4</option> <option>5</option> <option>6</option> <option>7</option> <option>8</option> <option>9</option> <option>10</option> <option>11</option></select>

how to change a selections options based on another select option selected?

Here is an example of what you are trying to do => fiddle

$(document).ready(function () {    $("#type").change(function () {        var val = $(this).val();        if (val == "item1") {            $("#size").html("<option value='test'>item1: test 1</option><option value='test2'>item1: test 2</option>");        } else if (val == "item2") {            $("#size").html("<option value='test'>item2: test 1</option><option value='test2'>item2: test 2</option>");        } else if (val == "item3") {            $("#size").html("<option value='test'>item3: test 1</option><option value='test2'>item3: test 2</option>");        } else if (val == "item0") {            $("#size").html("<option value=''>--select one--</option>");        }    });});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><select id="type">    <option value="item0">--Select an Item--</option>    <option value="item1">item1</option>    <option value="item2">item2</option>    <option value="item3">item3</option></select>
<select id="size"> <option value="">-- select one -- </option></select>

how to change a selections options based on another select option selected by vue js

Here is an example of how to do: https://codepen.io/antoniandre/pen/OJJQMGN?editors=1010

JS

new Vue({
el: '#app',
data: () => ({
selection1: null,
optionsList: ''
}),

methods: {
changeSelect2 () {
switch (this.selection1) {
case 'item1':
this.optionsList = '<option value="test">item1: test 1</option><option value="test2">item1: test 2</option>'
break;
case 'item2':
this.optionsList = '<option value="test">item2: test 1</option><option value="test2">item2: test 2</option>'
break;
case 'item3':
this.optionsList = '<option value="test">item3: test 1</option><option value="test2">item3: test 2</option>'
break;
}
}
}
})

HTML

<div id="app">
<select @change="changeSelect2" v-model="selection1">
<option value="item1">item1</option>
<option value="item2">item2</option>
<option value="item3">item3</option>
</select>

<select v-html="optionsList"></select>
</div>

But this is certainly not the cleanest way to do it I just converted your example as is.
To make it nicer, the options should be in a list probably.


Here is a little bit of tutorial to start and understand:

  1. include Vue library
  2. Never manipulate the DOM (unless no choice), Vue is DATA DRIVEN
  3. listen for DOM events on DOM elements with @eventName
  4. place your functions in methods, and your vars in data this is always like that
  5. the v-model attribute is a 2 way binding and triggers the variable to change automatically when the select list selected option changes, this is called "reactivity".
  6. place your variables in the html within v-html for HTML or mustaches {{ }} if you only want to keep its textual value and not HTML.
  7. follow a few tutorials

How to manipulate selected option's value to be the same as option text with JavaScript

You need to pass decisions[i] as second parameter in Option.

Syntax:new Option(text, value, defaultSelected, selected); More details Option

var reasons = {};    reasons['Order Inquiry'] = ['Order Status','Order Issue', 'X'];    reasons['Product Inquiry'] = ['Product Weight', 'Product Quality'];
function ChangeTypeList() { var typeList = document.getElementById("type"); var reasonsList = document.getElementById("reason"); var reasonsType = typeList.options[typeList.selectedIndex].value;
while (reasonsList.options.length) { reasonsList.remove(0); } var reason = document.getElementById('reason'); var decisions = reasons[reasonsType]; if (decisions) { var i; for (i = 0; i < decisions.length; i++) { //This line is changed var decision = new Option(decisions[i], decisions[i]); reasonsList.options.add(decision); console.log('decision', decision); } } }
<label for="type">Category<span class="required-input"> *</span></label>    <select id="type" name="type" onchange="ChangeTypeList()">        <option value="">--Select--</option>        <option value="Order Inquiry">Order Inquiry</option>        <option value="Product Inquiry">Product Inquiry</option>    </select>
<!-- Sub Category --> <label for="reason">Sub Category</label> <select id="reason" name="reason"></select>

keep selected option displayed when options based on another select

I can capture the value of the second < select > once submitted and use it in the remaining code

If I understand this correctly, you can generate the option's selected property using captured results in the following script area

"<option>item1...</option><option <?php echo .... 'selected'; ?> >item1...</option>"...

If .js file manipulation using php is not an option, using data attribute in the select element and retrieving it via .data() can work.

Then calling the $("#type").change() at the end of .ready() will executes the handler and construct the 2nd select box as usual.

 $("#type").change();

See Demo

Manipulate second <select> based on first <select> values using jQuery

I would use jQuery to add and remove options dynamically from the second optionset.

I found this article:

https://paulund.co.uk/add-and-remove-options-in-select-using-jquery

 In the gist is this:
// Remove options
$("#selectBox option[value='option1']").remove();

// Add options
$("#selectBox").append('<option value="option6">option6</option>');

I have seen this work in all browsers.

Change <select> options with javascript dynamically

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>

Select option with jquery based on other select option box

You can do simply something like this, using change() event handler