ade123 ade123 - 5 months ago 19
jQuery Question

Check if form fields have values on page load and if so, show labels?

I have a form where small labels are displayed above each field, once the user adds a value to that field.

This for is sometimes loaded with some of the fields being pre-populated.

How would i check on page load if any of the form fields have a value and if so, have the label visible?

Here's my current code for displaying labels once a field has a value:

$('.form-control').blur(function() {
if( $(this).val() ) {
$(this).prev().show();
}
});

Answer

on page load try this:

$('.form-control').each(function() {
 if( $(this).val() ) {
    $(this).prev().show();
 }
});