Demuri Celidze Demuri Celidze - 1 year ago 69
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 Source

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>