18

I have some problems with the browser Chrome, when it remembers password, username, etc. I cannot check if the input field is empty. I use the following code, but the if statement doesn't work in Chrome while it works in all other browsers.

$(document).ready(function() {    
    $('.inputWithLabel').each(function(){
        if ($(this).val().length != 0){
            var element_id = $(this).attr('id');
            $('label[for="' + element_id + '"]').addClass('inputWithText');
        }
    });
});

I tried to alter the following labels

<form action="" method="post">
    <div class="divInputLabel inputLabel">
        <label class="labelInput" for="username">Brugernavn*</label>
        <input class="inputLabel inputWithLabel" id="username" name="username" type="text">
    </div>
    <div class="divInputLabel inputLabel">
        <label class="labelInput" for="password">Adgangskode*</label>
        <input class="inputLabel inputWithLabel" id="password" name="password" type="password">
    </div>
    <div class="divInputLabel inputLabel">
        <label class="labelInput" for="password_again">Gentag adgangskode*</label>
        <input class="inputLabel inputWithLabel" id="password_again" name="password_again" type="password">
    </div>
    <div class="divInputLabel inputLabel">
        <label class="labelInput" for="first_name">Fornavn*</label>
        <input class="inputLabel inputWithLabel" id="first_name" name="first_name" type="text">
    </div>
    <div class="divInputLabel inputLabel">
        <label class="labelInput" for="last_name">Efternavn</label>
        <input class="inputLabel inputWithLabel" id="last_name" name="last_name" type="text">
    </div>
    <div class="divInputLabel inputLabel">
        <label class="labelInput" for="email">Email*</label>
        <input class="inputLabel inputWithLabel" id="email" name="email" type="text">
    </div>
    <div class="divInputLabel inputLabel">
        <input type="Submit" class="submit-button" value="Registre">
    </div>
</form>

I hope someone can help me.

bviale
  • 5,245
  • 3
  • 28
  • 48
user3805674
  • 221
  • 1
  • 5
  • Where in the document is this script included? – Mike Perrenoud Jul 30 '14 at 12:09
  • I've tried just after and before the none of it works. – user3805674 Jul 30 '14 at 12:40
  • 1
    Well it's not empty if it's prefilled by the browser. What is intended behavior? When page is loaded - do you want to reset all the fields? – Yuriy Galanter Jul 30 '14 at 13:43
  • I want to add a class for the label to change layout. The layout will be different, when label is not empty. When I remove the if statement, the function works fine, but all input fields get changed. I just don’t understand why the if statement won’t work in Chrome, but works in all other browsers. – user3805674 Jul 30 '14 at 14:03
  • Show your markup as well so we can see the structure you're trying to alter. – Palpatim Jul 30 '14 at 14:24
  • May be Chrome insert saved user data after some time after load document. Try use timeout to check fields. – CnapoB Jul 30 '14 at 14:51
  • 4
    I believe Chrome fires `change` event when prefilling form elements. You can try capturing it once to reset the values – Yuriy Galanter Jul 30 '14 at 16:08
  • 1
    Yuriy Galanter is right, I added this $('.inputWithLabel').change( function(){ var element_id = $(this).attr('id'); $('label[for="' + element_id + '"]').addClass('inputWithText'); }); which works in Chrome. – user3805674 Jul 31 '14 at 12:07
  • Does these text fields have value when page loads?? – Sindhoo Oad Oct 08 '15 at 13:48

10 Answers10

1

To check and see if the input is empty or not you can do something simpler like this.

$(document).ready(function() {
    $(".inputWithLabel").change(function() {
        if ($(this).val() == null || $(this).val() == "") {
            alert("Input is null!");
        } else {
            alert("Input is valid");
        }
    });
});
Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
Drew Gallagher
  • 746
  • 2
  • 9
  • 18
1

Don't change your code, apply this instead:

$(document).ready(function() {

    $('.inputWithLabel').each(function(){
        if ($(this).val().length != 0 && $(this).val().length != null){
            var element_id = $(this).attr('id');
            $('label[for="' + element_id + '"]').addClass('inputWithText');
        }
    });
});

Only set && $(this).val().length != null after $(this).val().length != 0 and it should run on different browsers. Try it!

john igneel
  • 367
  • 1
  • 4
  • 18
Rogerio de Moraes
  • 1,527
  • 18
  • 15
1

Try the following code:

    $(document).ready(function() {
      $('body .inputWithLabel').each(function(){
        if ($(this).val().length != 0){
          var element_id = $(this).attr('id');
          $('label[for="' + element_id + '"]').addClass('inputWithText');
        }
      });
   });

Just append body i.e $('body .inputWithLabel') in your each loop.

Alex Netkachov
  • 13,172
  • 6
  • 53
  • 85
1

Your Code looks fine... Label does not have .val() .. if we want innertext of label it should be .text() so replace .val() with .text() and it will work hopefully.... Also first append body i.e
$('body .inputWithLabel')

Adeel Shekhani
  • 995
  • 1
  • 11
  • 16
1

Try this:

$(document).ready(function() {

    $('.inputWithLabel').each(function() {
        if ($(this).val()) {
            var element_id = $(this).attr('id');
            $('label[for="' + element_id + '"]').addClass('inputWithText');
        }
    });
});
Raju Bera
  • 948
  • 8
  • 14
1

You can try it in your site

$('.inputWithLabel').each(function() {
    if ($(this).val() !== null && $(this).val() !== "") {
        var element_id = $(this).attr('id');
        $("#" + element_id).removeClass('inputWithLabel').addClass('inputWithText');
    }
});
Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
Binh LE
  • 387
  • 5
  • 17
1

I'd tried a similar thing. All you have to do is first turn your form id into a jQuery variable,

var $elem = document.getElementById("yourformid").value; //make sure your id has no # symbol
if ($elem > 0) {
    //do stuff
}

that should work.

Lucky
  • 16,787
  • 19
  • 117
  • 151
Curtis Chong
  • 783
  • 2
  • 13
  • 26
1

There is currently no possibility to detect if the form is filled by the browser autocompletion. Some alternatives I would suggest are:

Disable autocomplete: If you dont need it, this would be an easy solution

<input autocomplete="off">

or on the whole form:

<form autocomplete="off">

Using the change event to detect any change on the field:

$('#username').on('change keyup',function(){
    // to something
});

Here I can imagine to set a class if there is any change on the input field like:

$('#username').on('change keyup',function(){
    $(this).addClass('changed');
});

Or change the name attribute on the submitted field. e.g.: use this as default:

<input class="inputLabel inputWithLabel" id="username" name="usernameAutofill" type="text">

and change it later to this if anything is done with the input:

$('#username').on('change keyup',function(){
    $(this).attr('name', 'username');
});

But before you do any of this ask yourself following questions: Why you want to proof the Fields? What should happen if they are autofilled? What should happen if they are empty? What should happen if they are regulary filled?

Because what solution is the best depends pretty much on what exactly you want to do. If you answer this question we can help you more.

-2

try using:

$('').value

or

this.value

or

this.textContent

or

$('').innerHTML
-2

Your code is checking each input element which HAS some value, not the ones which are empty. Try changing your condition to:

if ($(this).val().length == 0){
...

this would add the class to the empty field label(s).

bwegs
  • 3,769
  • 2
  • 30
  • 33