60 465
Assignments Done
98,1%
Successfully Done
In April 2018

# Answer to Question #32907 in AJAX | JavaScript | HTML | PHP for Justin

Question #32907
I am having trouble with the code below because I am trying to call the text field input type so that whenever someone enters a number above 10 it responds with the alert shown below, but I am having a problem because whenever I input a number greater than 10 in the text field it still replies with &quot;the number is less than 10&quot;??? What am I doing wrong? FYI I am a beginner javascript learner //Variablesvar txtInput = document.getElementById(&#039;txtInput&#039;);//Functionsfunction submitNum(){   if (txtInput &lt;= 10)   {      alert(&quot;The number is less than 10&quot;);   }   else   {      alert(&quot;The number is greater than 10&quot;);   }}
Solution. &lt;html&gt;&lt;head&gt;&lt;title&gt;JavaSscript&lt;/title&gt;&lt;/head&gt;&lt;script type=&quot;text/javascript&quot;&gt;function submitNum(){    var txtInput = parseFloat(document.getElementById(&#039;txtInput&#039;).value);    if (txtInput &lt; 10)    {        alert(&quot;The number is less than 10&quot;);    }    else if(txtInput &gt; 10)    {        alert(&quot;The number is greater than 10&quot;);    }   else   alert(&#039;The number is equal to 10&#039;);}&lt;/script&gt;&lt;body&gt;&lt;input type=&quot;text&quot; placeholder=&quot;Type any number&quot; id=&quot;txtInput&quot; /&gt;&lt;input type=&quot;button&quot; onclick=&quot;submitNum()&quot; value=&quot;Submit&quot;  /&gt;&lt;/body&gt;&lt;/html&gt;Where have you done errors ?1.   When you do click, it calls only function submitNum() without other definitions. So you should insert needed variables in the function.2.   document.getElementById returns only DOM node (not a value from input tag), so you should take value. How can you do this ? That&rsquo;s easy &ndash; use property document.getElementById(&#039;txtInput&#039;).value. But it is not the end. It returns only string. To get a number use function parseFloat() or parseInt().

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!