JS: Change select option value when prompted?


Matthew

I looked around and no one has asked this question before.

I have a select box like this:

<select onchange="change()">
  <option value="" selected>Option 1</option>
  <option value="30">Option 2</option>
  <option value="90">Option 3</option>
</select>

I want to add another option...

  <option value="custom">Option 4</option>

...when selecting (clicking) a prompt box will pop up asking the user to replace the option value with multiple inputs (in the case of 30 or 90 there are no viable options because of the value in that option).

<script>
function change() {
  if(value == "custom") {
    value = prompt("Please enter a new number:", "60");
  }
}
</script>

I'm wondering the best way to do this is to use plain old javascript - I'd use jQuery if needed.

Any ideas? An example would also be nice.

Shama

Take a look at this code. I think this is what you are trying to do:

HTML

<select id="optionvals" onclick="change()">
  <option value="" selected>Option 1</option>
  <option value="30">Option 2</option>
  <option value="90">Option 3</option>
  <option value="custom">Option 4</option>
</select>

JS

function change() {
  var oItem = document.getElementById('optionvals');
  var value = oItem.options[oItem.selectedIndex].value;
  if(value == "custom") {
    alert("you've clicked b");
    value = prompt("Please enter a new number:", "60");
    oItem.options[oItem.selectedIndex].value = value;
    console.log(oItem.options[oItem.selectedIndex].value)
  }
}

This is done to prompt you only when the option selected value changes custom. Then, after selecting a custom value, it will rewrite that customoption element's value to the value you just entered in the prompt. After assigning the new value, I logged the value to show you that it is working.

Here is a fiddle : https://jsfiddle.net/ng7xvy05/

Related


JS: Change select option value when prompted?

Matthew I looked around and no one has asked this question before. I have a select box like this: <select onchange="change()"> <option value="" selected>Option 1</option> <option value="30">Option 2</option> <option value="90">Option 3</option> </select>

JS: Change select option value when prompted?

Matthew I looked around and no one has asked this question before. I have a select box like this: <select onchange="change()"> <option value="" selected>Option 1</option> <option value="30">Option 2</option> <option value="90">Option 3</option> </select>

How to change the value when the select option changes?

Shibbir Ahmed Below is my html code which is generated by php while loop: <select class="menu_extra_item"> <option>--Qnt--</option> <option>1</option> <option>2</option> <option>2</option> </select> <select class="menu_extra_item"/> <option

How to change the value when the select option changes?

Shibbir Ahmed Below is my html code which is generated by php while loop: <select class="menu_extra_item"> <option>--Qnt--</option> <option>1</option> <option>2</option> <option>2</option> </select> <select class="menu_extra_item"/> <option

HTML/JS: Change select option value in JavaScript

Ayural I have a dropdown select menu with two options. Basically, I'm using JavaScript to get the value of the selected dropdown option and paste it into a text area. Here is my code: $(document).ready(function () { $('#pdSev').change(function () { $('#t

HTML/JS: Change select option value in JavaScript

Ayural I have a dropdown select menu with two options. Basically, I'm using JavaScript to get the value of the selected dropdown option and paste it into a text area. Here is my code: $(document).ready(function () { $('#pdSev').change(function () { $('#t

HTML/JS: Change select option value in JavaScript

Ayural I have a dropdown select menu with two options. Basically, I'm using JavaScript to get the value of the selected dropdown option and paste it into a text area. Here is my code: $(document).ready(function () { $('#pdSev').change(function () { $('#t

HTML/JS: Change select option value in JavaScript

Ayural I have a dropdown select menu with two options. Basically, I'm using JavaScript to get the value of the selected dropdown option and paste it into a text area. Here is my code: $(document).ready(function () { $('#pdSev').change(function () { $('#t

HTML/JS: Change select option value in JavaScript

Ayural I have a dropdown select menu with two options. Basically, I'm using JavaScript to get the value of the selected dropdown option and paste it into a text area. Here is my code: $(document).ready(function () { $('#pdSev').change(function () { $('#t

Vue JS how to change variable value on selected select option

Kubavic I have 3 options in the selection and I want the textbox to appear when the third one is selected, how can I do that? I got this so far, but it doesn't work. <select asp-for="UploadOption" name="UploadOption"> <option value="1" selected="selected" na

Change selected value in select option

Justin J72: I am currently unable to change the value of the dropdown. I am currently adding items to the cart at https://www.reebok.com/cn/cart . I am trying to change the quantity. I am trying to rewrite java code to javascript. That being said. Select selec

Change selected value in select option

Justin J72: I am currently unable to change the value of the dropdown. I am currently adding items to the cart at https://www.reebok.com/cn/cart . I am trying to change the quantity. I am trying to rewrite java code to javascript. That being said. Select selec

JS option in select disabled on value?

data links How to disable option value instead of option index using JS. <select id="myselect"> <option value="20">20</option> <option value="30">30</option> </select> document.getElementById('myselect').options[0].disabled = true; document.getElementById('

select option generate value from ajax on change

Brownman Revival I have the code and flow for my project here , I have 3 selections here, one for continent, one for country, one for city, I get data from ajax request to populate these selections, it now works well, i just want to be a little fancy, so i wan

select option generate value from ajax on change

Brownman Revival I have the code and flow of my project here , I have 3 selections here, one for continent, one for country, one for city, I get data from ajax request to populate these selections, and now it runs Good, I just want to be a little fancy, so I w

Change value of select option based on checkbox

Pratyush Pranjal Whether the value of the selected option can be changed if the user checks the checkbox. If the user selects "four" from the selection options, its default value is "4". Now, if the user checks the checkbox, the select option value is "44". <s

select option generate value from ajax on change

Brownman Revival I have the code and flow for my project here , I have 3 selections here, one for continent, one for country, one for city, I get data from ajax request to populate these selections, it now works well, i just want to be a little fancy, so i wan

React select option value change variable data

Everson Carvalho I'm trying to be a front-end developer, so I'm creating a project with company information. I've tried to find stuff on YouTube and here, but I still can't get it. I want to use Change to display the result, but how can I pass the option value

Change Div class based on value in select option

Alpha X I have a select dropdown with options and I want to display a certain div based on the selection in the dropdown . Technically, I believe the easiest way to do this is to set a div that shouldn't be visible by applying the correct css class to display:

React select option value change variable data

Everson Carvalho I'm trying to be a front-end developer, so I'm creating a project with company information. I've tried to find stuff on YouTube and here, but I still can't get it. I want to use Change to display the result, but how can I pass the option value

select option generate value from ajax on change

Brownman Revival I have the code and flow of my project here , I have 3 selections here, one for continent, one for country, one for city, I get data from ajax request to populate these selections, now it runs Good, I just want to be a little fancy, so I want

Change value of select option based on checkbox

Pratyush Pranjal Whether the value of the selected option can be changed if the user checks the checkbox. If the user selects "four" from the selection options, its default value is "4". Now, if the user checks the checkbox, the select option value is "44". <s