JavaScript/HTML - Add up the values from dropdown list and textbox

how to display selected value of dropdownlist in textbox using javascript
how to find the selected value of dropdown in javascript
set selected value of dropdown in javascript
add input fields in form when dropdown option selected
get selected value of dropdown in javascript on button click
how to get dropdown selected value in textbox using jquery
dependent drop down list html w3schools
html.dropdownlist get selected value javascript

just want to ask that how to extract the values from dropdown list and from textbox, then total up them together and display the total? I googled around and tried different ways but it did not worked. Still learning in HTML and JavaScript.

What is your height? (centimetres)
<select>
  <option name="height1" value="100">100cm</option>
  <option name="height1" value="101">101cm</option>
  <option name="height1" value="102">102cm</option>
  <option name="height1" value="103">103cm</option>
  <option name="height1" value="104">104cm</option>
  <option name="height1" value="105">105cm</option>
</select>
<br><br> What is your weight? (kg)
<input type="number" id="weight1" step="0.01" name="weight" pattern="\d{4}" autocomplete="off"><br><br> BMI: <input type="text" id="txtresult" name="bmi" readonly>
<input type="button" name="calculate" value="Calculate" onclick="calculate_bmi()">

To get the value from the dropdown/select element, you can use:

document.querySelector('select').value;

This will give you a string (words), however, since you want to do calculations with this input you need it to be a number (integer, float etc...) so we can use a + in front of this statement to convert it to a number:

+document.querySelector('select').value;

However, I recommend that you add an id to your select element so you can target it like so (adding an id will improve your code's runtime):

+document.getElementById('height').value;

To get the value from your textbox, you can do:

+document.getElementById('weight1').value;

Here we are also converting it to a number by using the + operator.

Lastly, to set the bmi's textbox to the calculated value you can use:

document.getElementById("txtresult").value = bmi;

See working example below:

function calculate_bmi() {
  let height = +document.getElementById("height").value;
  let weight = +document.getElementById("weight1").value;
  let bmi = height + weight; // perform bmi calculation here
  document.getElementById("txtresult").value = bmi;
}
What is your height? (centimetres)
<select id="height">
  <option name="height1" value="100">100cm</option>
  <option name="height1" value="101">101cm</option>
  <option name="height1" value="102">102cm</option>
  <option name="height1" value="103">103cm</option>
  <option name="height1" value="104">104cm</option>
  <option name="height1" value="105">105cm</option>
</select>
<br><br> What is your weight? (kg)
<input type="number" id="weight1" step="0.01" name="weight" pattern="\d{4}" autocomplete="off"><br><br> BMI: <input type="text" id="txtresult" name="bmi" readonly>
<input type="button" name="calculate" value="Calculate" onclick="calculate_bmi()">

How to retain the selected value in the dropdown after page refresh ?, How do I show the selected values in a multiple DropDownList? Definition and Usage. The add() method is used to add an option to a drop-down list. Tip: To remove an option from a drop-down list, use the remove() method.


You can use document.getElementsByName("height1")[0] to get the desired element, then use h.options[h.selectedIndex].value to find the selected value. Then, you can use parseFloat as usual:

function calculate_bmi() {
  var h = document.getElementsByName("height1")[0];
  h = parseFloat(h.options[h.selectedIndex].value);
  // h = h / 100;
  var w = parseFloat(document.getElementById("weight1").value);
  console.log(h, w);
  var result = h + w;
  document.getElementById("txtresult").value = result.toFixed(2);
}
What is your height? (centimetres)
<select name="height1">
  <option value="100">100cm</option>
  <option value="101">101cm</option>
  <option value="102">102cm</option>
  <option value="103">103cm</option>
  <option value="104">104cm</option>
  <option value="105">105cm</option>
</select>
<br><br> What is your weight? (kg)
<input type="number" id="weight1" step="0.01" name="weight" pattern="\d{4}" autocomplete="off"><br><br> BMI: <input type="text" id="txtresult" name="bmi" readonly>
<input type="button" name="calculate" value="Calculate" onclick="calculate_bmi()">

JavaScript/HTML, How do I keep the selected value of dropdown after page refresh? I recently had to add a dropdown box to a textbox at work, instead of jumping on the first jQuery plugin I could find, I instead started checking if this feature was finally added to "native html", and it was, and it is called a datalist. Simple Example A datalist in HTML5 is a simple way giving a (textbox) input field a dropdown of choices to select from, you bind a datalist and an input


If you are trying to get the value from the dropdown list. Get the Select element by name which stores it as an array, then run a for loop through the list and extract the value. See below,

function calculate_bmi() {
    var value = 0;
    var h = document.getElementsByName("height1");
    for(var i = 0; i<h.length; i++){
        if(h[i].selected){
            value = h[i].value
            // Do something with the value
        }
    }
}

Javascript HTML element get selected select option text value , JavaScript/HTML - Add up the values from dropdown list and textbox. how to display selected value of dropdownlist in textbox using javascript how to find the​  Adding options to List Box in client side Javascript Options to a drop down list box can be added dynamically using client side JavaScript. We will discuss a simple way of adding options to a list. The process of adding can be controlled based on different condition required. Depending on conditions options can be removed also.


This code worked for me. I modified your html a bit and added an id to the select box.

  function calculate_bmi() {
          /*var h = parseFloat(document.getElementById("height1").value);
    	var w = parseFloat(document.getElementById("weight1").value);
    	if ((h != parseFloat(h, 10)) || (w != parseFloat(w, 10)))
    		alert("Must insert number")
    	if ( ( h <= 0 ) || ( w <= 0 ) )
    		alert("Please insert positive numbers")
    	if ( ( h >= 2.7 ) || ( w >= 500 ) )
    		alert("Out of range");
    	else
    	{
    		var result = w / h / h;
    		result = Math.round(result * 100) / 100;
    		document.getElementById("txtresult").value = result;
    	}*/
          var selectBox = document.getElementById("selectBox");
          var h = parseFloat(selectBox.value);
          //h = h / 100;
          var w = parseFloat(document.getElementById("weight1").value);
          var result = h+w;
          document.getElementById("txtresult").value = result.toFixed(2);
        }
 <html>
      <body>
        What is your height? (centimetres)
        <select id="selectBox">
          <option name="height1" value="100">100cm</option>
          <option name="height1" value="101">101cm</option>
          <option name="height1" value="102">102cm</option>
          <option name="height1" value="103">103cm</option>
          <option name="height1" value="104">104cm</option>
          <option name="height1" value="105">105cm</option>
        </select>
        <br /><br />
        What is your weight? (kg)
        <input
          type="number"
          id="weight1"
          step="0.01"
          name="weight"
          pattern="\d{4}"
          autocomplete="off"
        /><br /><br />
        BMI: <input type="text" id="txtresult" name="bmi" readonly />
        <input
          type="button"
          name="calculate"
          value="Calculate"
          onclick="calculate_bmi()"
        />
      </body>
      <script>
      
      </script>
    </html>

HTML DOM Option text Property, html 5 and javascript tutorial on get selected text from the select option element dropdownlist Duration: 5:06 Posted: Apr 16, 2018 How to auto populate textbox value from database based on selecting related value from dropdown list how to use if condition in dropdown selected index change event Save multiple dropdown select value by using chosen-select jquery


HTML DOM Option value Property, Change the text of an option element in a drop-down list: Tip: If the value property is not specified for an option element, then the text content will be sent to the  What I am trying to do is to populate an html dropdown list with values from a file. I also need it coded to where it will look in the first field for a value from one dropdown box and then populate another dropdown box. I am trying to understand Java script, but I am not sure if I would be able to do this with Java Script.


HTML DOM Select add() Method, Example. Alert the value of the selected option in a drop-down list: var x = document.getElementById("mySelect").selectedIndex; alert(document. How to Add Dropdowns to Textboxes in HTML5 Dynamically add textbox, radio button in html using Changing an Access text box into a combo box to provide a drop-down list of values


How to add dropdowns to textboxes in HTML5, Tip: To remove an option from a drop-down list, use the remove() method. Browser Support. Method. add(), Yes, Yes, Yes, Yes, Yes  Hi, I have a Textbox and dropDownlist. I am showing database values into the dropdown list. When user enters something into database. And if entered value is existing in the dropdownlist alert shou