Jquery - Identify automatic text

Asked

Viewed 110 times

0

I am using Jquery to identify when the text box is text in the text box, thus:

CSS:

.inputtext {
    border: 1px solid lightgrey;

    margin: 0;
    margin-top: 11pt;

    width: 100%;
}

.inputtext:focus {
    border: 1px solid green;
    box-shadow: 0 0 1px green;
}

.inputtext + span {
    font-size: 15pt;
    position: absolute;
    left: 15px;
    top: 18pt;
    color: grey;
    cursor: text;
    transition: all 200ms ease;
}

.inputtext:focus + span {
    font-size: 11pt;
    color: green;
    transform: translate(-10px,-18pt);
}

.inputtyped + span {
    font-size: 11pt;
    transform: translate(-10px,-18pt);
}

HTML:

<label>
    <input type="email" name="email" id="email" class="inputtext" required>
    <span>Email</span>
</label>

Jquery:

function typed() {
    var val = $(this).val();

    if (!val) {
        $(this).removeClass('inputtyped');
    } else {
        $(this).addClass('inputtyped');
    }
}

$(window).on('load', function () {
    $("input.inputtext").each(function (index, element) {
        $(element).change(typed);
        $(element).focus(typed);
        $(element).trigger('change');
    });
});

It works perfectly, like this:

<code>span</code> dentro do <code>input</code> quando não estiver selecionado e sem texto <code>span</code> fora do <code>input</code> e verde quando estiver selecionado <code>span</code> fora do <code>input</code> quando não estiver selecionado e com texto

However, when the browser fills in with the saved email and password, the span is not adjusted:

<code>span</code> dentro do <code>input</code>, mesmo havendo texto

How can I fix this?

  • Cara vc can solve this by putting in HTML autocomplete="off" input, It is not always the best solution, but sometimes it can suit you... https://www.w3schools.com/tags/att_input_autocomplete.asp Do a test ai

  • @hugocsl by what I understood and looked at the example, the autocomplete="off" will disable the autocomplete that happens when you start typing, just like Ides do. It doesn’t help me in this case

2 answers

2


This problem of autocomplete is a little complicated to solve because it varies from browser to browser and also in different versions of these.

Chrome for example does not indicate that you are filling in the fields when you read the page

A possible solution will be the following:

$(window).on("load", function() {

    //Verifica alterações a cada 1 segundo (1000 millisegundos)
    var verificador = setInterval(function () {

        $("input.inputtext").each(function(index, element) {
        $(element).change(typed);
        $(element).focus(typed);
        $(element).trigger("change");
    });

    }, 1000);

});

Basically we are creating a timer that runs every 1 second (can change time to your liking) and checks the field.

An option is also to cancel the timer after a few seconds so it is not constantly running:

clearInterval(verificador);

I’m still working on it

I hope I’ve helped.

Regards

0

I don’t understand the "automatically puts the text". Otherwise, it takes care?

function typed() {
  var val = $(this).val();

  if (! val) {
    $(this).removeClass('inputtyped');
  } else {
    $(this).addClass('inputtyped');
  }
}

$(window).on('load', function() {
  $('input.inputtext').each(function(index, element) {
    $(element).on('change', typed);
    $(element).on('focus', typed);

    typed.call(element);
  });
});
  • It does not solve the typed() use the selector $(this). "automatically puts the text" refers to when the browser saves the login, so it automatically fills the text fields.

  • I edited my answer. See now.

  • It still doesn’t work when I enter the page, but if I reload it or edit something, it works.

  • This is weird. Can you tell me what behavior causes this?

  • I have no idea. It’s like the $(window).on('load') only occurs when there is any change on the screen

Browser other questions tagged

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