Search code examples
javascriptdropdowncalculation

Using the dropdown botton for calculating and put the value in formula


I'm beginner in JavaScript. I want to use the drop-down button for calculateing but I don't know how I can take the value of choices and put them in calculation. Is it possible to help me that how I can do it and put the values in formula?

<select id="Selectpaint">
  <option selected disabled>نوع رنگ اکریلیک</option>
  <option value="8">رنگ اکریلیک طلایی</option>
  <option value="8">رنگ اکریلیک متالیک صدفی</option>
  <option value="13">رنگ اکریلیک مات  </option>
  <option value="14">رنگ اکریلیک نیم براق   </option>
  <option value="13">رنگ اکریلیک براق</option>
  <option value="10">رنگ اکریلیک آستری</option>
  <option value="12">مادر رنگ اکریلیک</option>
 </select>
 </p>


  <p>

Maybe after taking the value, I use it in formula which is contained different number that user put in different fields.


Solution

  • Execute a JavaScript when a user changes the selected option of a element

    HTML

    <select id="mySelect" onchange="myFunction()">
      <option value="Audi">Audi</option>
      <option value="BMW">BMW</option>
      <option value="Mercedes">Mercedes</option>
      <option value="Volvo">Volvo</option>
    </select>
    

    JavaScript

    function myFunction() {
      var x = document.getElementById("mySelect").value;
      // use your formula here
    }
    

    So here x will contain the selected value. You can use this x value and add it in the calculation function.