html - Calculate Total Onclick Javascript -
i learning javascript , still new language. in html code have 2 lists. in first select list select product want in second select amount of product require.
i have attempted write javascript code following:
- get product value , assign variable
- get nr of product , assign variable
- multiply product value number of product
- when user clicks submit display alert box total
however code not working when user click submit button message nan instead of result of total amount variable please can have @ code , tell me doing wrong
<script type="text/javascript"> function calc() { var total; var fruitorveg; var nroffruit; course = document.getelementsbyname("fruitorveg.course.value") nroffruit = document.getelementsbyname("nroffruit") total = fruitorveg * nroffruit; window.alert(total) } </script>
you taking values strings (rather numbers), , weren't using "fruitorveg". (noticed later/ new comments) using names when should use id's elements want javascript data directly , specifically, , update js accordingly. try this:
function calc() { var fruitorveg = number(document.getelementsbyid("fruitorveg").value); var nroffruit = number(document.getelementsbyid("nroffruit").value); var total = fruitorveg * nroffruit; window.alert(total) }
Comments
Post a Comment