javascript - Works in .js but not in jQuery code, why? -


this 1 won't work (jquery)

function convert(degree) {      var temp;      if (degree == 'c') {         temp = $('#c').val(temp * 9 / 5 + 32);         $('#f').val(temp);      } else {         temp = $('#f').val(temp - 32 * 5 / 9);         $('#c').val(temp);     } } 

this 1 works (javascript)

function convert(degree) {     var temp;     if (degree == 'c') {         temp = document.getelementbyid("c").value * 9 / 5 + 32;         document.getelementbyid("f").value = temp;     } else {         temp = document.getelementbyid("f").value - 32 * 5 / 9;         document.getelementbyid("c").value = temp;     } } 

html code here

<h1>temperature converter</h1>  <p><input id="c" onkeyup="convert('c')"> &deg; celsius</p>  <p><input id="f" onkeyup="convert('f')"> &deg; fahrenheit</p>  

you not using .val() correctly. returns value when there no parameters, sets otherwise. this:

function convert(degree) {      var temp;      if (degree == 'c') {         temp = $('#c').val() * (9 / 5) + 32;         $('#f').val(temp);      } else {         temp = $('#f').val() - 32 * (5 / 9);         $('#c').val(temp);     } } 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -