How to add event listener to input tag and perform some kind of conditions and display something using javascript | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to add event listener to input tag and perform some kind of conditions and display something using javascript

I want to read number from input field html if number entered in input field is negative for ex- -5 display "enter a positive number " using javascript https://code.sololearn.com/WVtdH5HhdhNF/?ref=app

18th Aug 2022, 8:34 AM
Shelly Kapoor
Shelly Kapoor - avatar
15 Answers
+ 2
Shelly Kapoor I said call function on click event. You have to add a button and add click event on that button. And do other operation in that function like get value and check condition.
18th Aug 2022, 10:05 AM
A͢J
A͢J - avatar
+ 2
Shelly Kapoor Check the spelling of myFunction var number=document.getElementById("someid"); Write this line inside function and You are not getting value from this element There should be if (number.value < 0) https://code.sololearn.com/WATz0CAH2eLL/?ref=app
18th Aug 2022, 10:41 AM
A͢J
A͢J - avatar
+ 2
Shelly Kapoor Did you check my shared code?
18th Aug 2022, 12:06 PM
A͢J
A͢J - avatar
+ 2
Shelly Kapoor Check my code again. That is working as you want. I think you didn't check with different input
18th Aug 2022, 10:23 PM
A͢J
A͢J - avatar
+ 1
Shelly Kapoor make a function and call that function on click event. Perform the task inside that function.
18th Aug 2022, 9:01 AM
A͢J
A͢J - avatar
+ 1
Ipang yes it's fine but how to output the value based on condition outside the input field I have use <p> tag and using document.getElementById.innerHTML I am trying to output but it is not outputing
18th Aug 2022, 11:38 AM
Shelly Kapoor
Shelly Kapoor - avatar
+ 1
A͢J ,Ipang https://codepen.io/shelly-kapoor/pen/zYWyMZX I have solved it using event listener but if number is equal to even I want to print 2,4,6 if user type 2,if user type 4, I want 4,6,8
18th Aug 2022, 6:12 PM
Shelly Kapoor
Shelly Kapoor - avatar
0
A͢J I want to read value from input tag not onclick when I enter any value it reads That and display something based on condition outside the input field.If possible could you refractor my code.I have made my logic already but needs to read from input field
18th Aug 2022, 9:06 AM
Shelly Kapoor
Shelly Kapoor - avatar
0
A͢J please check I have made some changes it is not displaying output "enter a positive number "after reading value from input field
18th Aug 2022, 10:12 AM
Shelly Kapoor
Shelly Kapoor - avatar
0
Shelly, If you want to check as any input was typed in; then "oninput" is probably what you wanted. https://www.w3schools.com/jsref/event_oninput.asp
18th Aug 2022, 11:30 AM
Ipang
0
Shelly, First understand that input value is generally a string value, thus we need to convert it into a numeric value using Number() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number And then check whether it is a valid numeric data using isNaN() https://developer.mozilla.org/id/docs/Web/JavaScript/Reference/Global_Objects/isNaN Once the data is validated, then you can proceed with your own numeric validation - positive/negative, odd/even
18th Aug 2022, 11:48 AM
Ipang
0
And assign some data to an element's innerHTML attribute to change its content document.getElementById("output").innerHTML = "Enter a positive number";
18th Aug 2022, 11:50 AM
Ipang
0
It is easy in saying can you implement it?
18th Aug 2022, 11:57 AM
Shelly Kapoor
Shelly Kapoor - avatar
0
Shelly, Yes, but I am sure you can do this so don't give up now that you're so close ...
18th Aug 2022, 12:00 PM
Ipang
0
Yes,AJ In it what I want is when you enter input value like -3 it should display at that time exactly outside the box saying "Enter any positive number".So how to do this I think it will not work with function I have to add event listener https://code.sololearn.com/WRZNGEtFhbBQ/?ref=app
18th Aug 2022, 12:08 PM
Shelly Kapoor
Shelly Kapoor - avatar