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!

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

8/18/2022 8:34:11 AM

Shelly Kapoor

15 Answers

New Answer

+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.

+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

+2

Shelly Kapoor Did you check my shared code?

+2

Shelly Kapoor Check my code again. That is working as you want. I think you didn't check with different input

+1

Shelly Kapoor make a function and call that function on click event. Perform the task inside that function.

+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

+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

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

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

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

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

0

And assign some data to an element's innerHTML attribute to change its content document.getElementById("output").innerHTML = "Enter a positive number";

0

It is easy in saying can you implement it?

0

Shelly, Yes, but I am sure you can do this so don't give up now that you're so close ...

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