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.
I have a code like this:
<div><input type="text" value=""></div>
How do I check with JS or jQuery if input contain any data (even from localstorage or browser cache) on load and add class to parent div if not empty?
<input>
Try this
//trim() is used to remove extra spaces in your input value if($('#ver_input').val().trim() != ''){ $(this).parent().addClass('any-class'); }
html:
<div><input type="text" value="" id="ver_input"></div>
Add a comment
To check if the value of an input is not empty, use the following:
if($("input").val()) { ... }
If there are multiple inputs you may want to put an id on this input, but this code works if there is just one input.
var input = document.querySelector('input'); var yourDiv = document.querySelector('div'); if(input.value === ''){ yourDiv.classList.add("foo"); }
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.
<input>element