Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
Is there anyway I can check if an input is empty using javascript and then run a function? I'd like it to be in the input itself if possible.
There is onChange so is there an ifEmpty lol?
onChange
ifEmpty
if (myElement.value == '') {runFunction();}
<input onChange="this.value.trim().length > 0 && myFunction()" />
Add a comment
Try this
function a(e) { if(e.value=="") return false; else console.log('there is a value') }
<input value="" onclick="a(this)">
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.
if (myElement.value == '') {runFunction();}JavaScript validation for empty input fieldifEmptyattribute/event natively implemented, you need to take care of that by checking whether, on change, the value is effectively "empty" (define empty, anyway). Besides, can you please show what you've tried to accomplish the goal?