Set CSS style for already filled fields

Asked

Viewed 44 times

3

I have the following javascript function:

 $('input').blur(function() {
  var $this = $(this);
  if ($this.val())
    $this.addClass('used');
  else
    $this.removeClass('used');
});    

she adds the class used to the element input clicked if it has value, if not, when losing focus it is without class used. works properly however these fields are filled with a value that comes from the database, and as it does not occur the click input it does not add class used, how can I fix this?

1 answer

3


When opening your HTML, check all inputs and add used to each one that is filled using .each():

$(document).ready(function() {
    $("input[type='text']").each(function( index ) {
        if ($this.val())
            $this.addClass('used');
        else /* Este else é opcional. */
            $this.removeClass('used');
    });
});
  • perfect! thank you very much! worked!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.