0
How to assign the string of dropdown, a numeric value so that when clicked it fills the input field which can be used in calcula
there is dropdown with 9 different options. each option is string but carries numeric value. when the options are clicked the number goes to input fields and from that input field, calculating function takes it to perform calculation. Can anyone help me with the javaScript required in the program?
7 Answers
+ 3
Use 'oninput' event on <select> element, and read/use properties of selected option in the way your need: extracting numeric value from option text, or using 'value' attribute... Anyway, 'value' attribute is of type string, so you need to convert it to number to use it in calculation:
var num = "42";
var numFloat = parseFloat(num);
var numInt = parseInt(num);
var numNum = Number(num); // best suited numerical type
Check this very basic code example for more:
https://code.sololearn.com/Wzh0273rEIxk/?ref=app
+ 3
Don't worry I'm making a better code for your question
0
Thank you! got it working.đđđđ
- 1
Thanks! Got some idea.but still could not figure it out.
- 1
here is a quick way of using onchange with dropdowns
https://code.sololearn.com/W6v8TCK1ojA1/#html you could change the alert to change an element in the page and proform calculations
- 2
add an onclick function to the options and have a value inside the Parentheses to pass to the function
eg onclick = function('option1'), onclick = function('option2') and so on,
then have the function code to look somthing like this
function(numericValue){
document.getElementById('input field to change value onclick').value = numericValue
}
you may want to add more functions inside braces to perform the calculations and update the page in other places aswell