How to check if several dynamically created input is empty?

Asked

Viewed 167 times

0

I have this Input that is generating via Knockoutjs:

<input type="text" class="req" placeholder="" data-bind="value:Request" />

But I have to check if it is empty and if I am applying a specific css: As it can be created several equal fields, I can only apply in the first field my validation.

My Javascript:

if ($(".req").val() == "") { 
    $(".req").addClass(validationClassErrosType.input)
}
validationCheckImage($("#req"), $("#req").parent());

The image of the input:

inserir a descrição da imagem aqui

Well he always takes the first class. how could he do this via Avascript?

2 answers

2


I made a basic example with jQuery, see if you can adapt to your case.

$('#testarInputs').click(function(){
  $('.teste').each(function(){
    if(this.value == ''){
      $(this).css('border','1px solid red');
    }else{
      $(this).css('border','');
    }
  }); 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="teste">
<input class="teste">
<input class="teste">
<input class="teste">
<input class="teste">
<input class="teste">
<input class="teste">
<input class="teste">
<input class="teste">

<input type="button" id="testarInputs" value="Testar">

  • Now make it work. Thank you!

1

Try this :

for(var x = 0; x < $('.req').length; x++){
 if($('.req')[x].val() === ""){
  console.info("Adicionar Css");    
 }
}
  • good is not well to catch the first element I want to catch all that go putting the class in what is empty.

Browser other questions tagged

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