Exchange input class when completed

Asked

Viewed 349 times

1

I’m making a form using primefaces, and I wanted my input to change class when it was filled, only going back to the previous class if the user deleted the content. This is to make the outputLabel come out of the input and be visible according to the figure attached.

The first figure is as it is initially; The second figure is how to stay; The third is how it’s getting.

Does anyone know a way to do that? Mine’s not working...

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • If I understand your question correctly, the way to do this using Primefaces is with Watermark. See here: https://www.primefaces.org/showcaseui/misc/watermark.xhtml

  • Luan, post the code too, is easier to analyze, including the code you are trying.

2 answers

0

You can do a check on the input Blur.

See which class is on the label when it is on top of the input (with the effect you want) and do the following:

IF YOU ARE USING JQUERY

$('#selector_input').on('blur', function(){
    var _self = $(this);

   if(_self.val() != null){
      $('#selector_label').addClass('class-do-efeito');
   }else{ 
      $('#selector_label').removeClass('class-do-efeito');
   }
})

IF YOU ARE USING PURE JAVASCRIPT

var input = document.getElementById('id_input');
var label = document.getElementById('id_label');

input.onblur = verifyInput(input);

function verifyInput(input){

   if(input.value != null){
      label.classList.add('class-do-efeito');
   }else{
      label.classList.remove('class-do-efeito');
   }
}
  • I’m not using jquery :(

  • @Luansilva updated the answer with the solution using pure javascript. Good luck ;)

  • hasn’t worked yet!! I think it might be the place I’m putting... is it necessary to call this function?? And I put where? in a script within the same page or another external one (if it makes a difference)

  • @Luansilva, I updated the code again. See if it now works :) You can put in an external file (if there is one already) or in the same file, as it is a relatively small code, I recommend putting in the same file (tag script before the body closes).

  • ta almost there... he is already coming with the class even without having anything written

  • Could you post your code to Jsfiddle? I think it would be better to support

  • I’ve already solved... Fight!!

Show 2 more comments

0

If you’re just changing the class of input (as a question), use oninput (the oninput is more efficient because it detects any changes in the field):

<script>
    var el = document.querySelector("#id_do_input");
    el.oninput = function(){
        this.value ? this.classList.remove('nome_da_class') : this.classList.add('nome_da_class')
    }
</script>

Example:

var el = document.querySelector("#id_do_input");
el.oninput = function(){
   this.value ? this.classList.remove('nome_da_class') : this.classList.add('nome_da_class');
}
.nome_da_class{
  background: yellow;
}
<input placeholder="Nome" type="text" class="nome_da_class" id="id_do_input" />

  • does not have the "oninput" pro inputText of the first faces

  • @Luansilva I updated the answer. Abs!

  • I managed to resolve by changing your onInput to onChange... It’s working. Thank you!

Browser other questions tagged

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