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')"> ° celsius</p> <p><input id="f" onkeyup="convert('f')"> ° 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
Post a Comment