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 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

30th May 2017, 9:49 PM
Sandesh Tamang
Sandesh Tamang - avatar
7 Answers
30th May 2017, 11:03 PM
Nomeh Uchenna Gabriel
Nomeh Uchenna Gabriel - avatar
+ 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
31st May 2017, 5:13 AM
visph
visph - avatar
+ 3
Don't worry I'm making a better code for your question
31st May 2017, 6:47 AM
Nomeh Uchenna Gabriel
Nomeh Uchenna Gabriel - avatar
0
Thank you! got it working.😇😇🙂😀
31st May 2017, 12:58 PM
Sandesh Tamang
Sandesh Tamang - avatar
- 1
Thanks! Got some idea.but still could not figure it out.
31st May 2017, 6:46 AM
Sandesh Tamang
Sandesh Tamang - avatar
- 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
31st May 2017, 7:05 AM
Graham Caswell
Graham Caswell - avatar
- 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
30th May 2017, 11:05 PM
Graham Caswell
Graham Caswell - avatar