my function() is not working or the value is not converting to a number

Multi tool use
Multi tool use


my function() is not working or the value is not converting to a number



Been stuck here ALL day long trying to figure out what is wrong with my syntax.
Here’s my code… I’d like to verify if Number1 is > than Number 2. However, whenever I tried to run the code I always getting false.



Please help me..



PS. I am new to coding :(




var num1 = document.getElementById('num1');
var num2 = document.getElementById('num2');

var x = Number(num1);
var y = Number(num2);

function higherThan(x, y){
x > y? alert(true) : alert(false);
}


Numb1: <input type="number" id="num1"><br>
Numb2: <input type="" id="num2"><br>
<button onclick="higherThan();">Is Higher Than?</button>





document.getElementById('num1') gets the whole element. Change it to document.getElementById('num1').value to get the value
– Manually Overridden
Jul 1 at 5:54


document.getElementById('num1')


document.getElementById('num1').value




3 Answers
3



Get the values of the inputs inside the function.In your case you are trying to get the value even before user have provided any input.
Also to get the value you need to do


document.getElementById('elemId').value




function higherThan(x, y) {
var num1 = document.getElementById('num1').value;
var num2 = document.getElementById('num2').value;

var x = Number(num1);
var y = Number(num2);
x > y ? alert(true) : alert(false);
}


Numb1:
<input type="number" id="num1">
<br> Numb2:
<input type="" id="num2">
<br>
<button onclick="higherThan();"> Is Higher Than ?</button>



a few things:


higherThan()


higherThan()


num1


num2


parseInt


var x = Number(num1);



You have to take the value from the control. You can pass the id's of the control's to refer that inside the function for getting the values. Try the following way:


value


id




function higherThan(n1, n2){
var x = Number(document.getElementById(n1).value);
var y = Number(document.getElementById(n2).value);
var res = x > y? true : false;
alert(res);
}


Numb1: <input type="number" id="num1"><br>
Numb2: <input type="" id="num2"><br>
<button onclick="higherThan('num1','num2');">Is Higher Than?</button>





I followed your codes above and it is working now.. except that your declared variables x and y has the getElementById of X and Y. should it be the two id of two texts? but thank you so much for the help. will keep these in mind. var x = Number(document.getElementById('num1').value); var y = Number(document.getElementById('num2').value);
– SYoung123
Jul 1 at 6:23







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

n0hv,U6nBMFaseH7sZP Pom8T2W6 R,80g
JXlni L6lH2g wgRF 5Svy 40RB zx5GubG XP QM,dXarq4hrNgCsU,lcTVKL8bBrZYtoRQ

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

django NoReverseMatch Exception

List of Kim Possible characters