Demuri Celidze Demuri Celidze - 2 months ago 12
HTML Question

How to check if input is not empty on load?

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?

Answer

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>
Comments