How to force element state of the HTML template?

Asked

Viewed 81 times

0

Using the CSS framework Materialize, I came across the following situation. I own some fields <input> with <label> as the example:

<label for="name">Nome</label>
<input type="text"
       formControlName="name"
       id="name"/>

What generates this result:

Campo com o label ocupando a área de texto como padrão

So I changed the element <label> adding the class .active, as shown in documentation:

<label for="name" class="active">Nome</label>
<input type="text"
       formControlName="name"
       id="name"/>

What generates the desired result:

campo modificado para o resultado desejado exemplo do resultado desejado

However, if I select the empty field and click out of it without writing anything it goes back to its original state. How can I force the field to the "active" state (force the page behavior to as if the field was filled)?

  • 1

    If your code is the same in the two references in your question, shouldn’t you have a class? Another thing, take a look at this, see if it solves, I believe that the field should have a placeholder as in the last image... https://answall.com/questions/272038/materialize-css-label-fixo-topo/272057#272057

  • @hugocsl thanks, I added the class. On the link, solved my problem. But there is a way to force the state of a component in the way I described?

  • 1

    Puts an empty placeholder in the input: <input placeholder ...>

  • 1

    Yes, in the Blur you go back to class like <input type="text" onblur="this.classList.add('ativo');"> has to see if this will have more force than the JS that removes the class, if it does not work puts in a separate js and indexes after the js of Materialize. Maybe with the empty placeholder like @sam said too...

  • I haven’t tried it but suddenly it might help. Supposing you have one FormGroup with the name meuFormGroup, you could use the property touched of FormControl nome to inject the CSS class: <input [ngClass]="{ active: meuFormGrou.controls.name.touched }" type="text" formControlName="name" id="name" />. The touched will only be true when the event onBlur is fired into the component.

No answers

Browser other questions tagged

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