Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery - Select empty text input and add border for them

I want to add a border for empty text input fields. So I wrote a following code. But it is not working. So i used alert box for get to know how many selectors are select. But it is return 0. What is my wrong?

 $("input[type=submit]").click(function(){
       var empty = $('input:text[value=""]');
       alert(empty.length);

       if(empty.length >0){

           $("form").effect("shake",{
               times:3,
               distance : 50         

           },450,function(){
           $('input:text[value=""]').each(function(){
              $(this).css("border","1px solid red"); 
           });
           });



           return false;
       }

   });
like image 509
Nadishan Avatar asked Mar 28 '26 21:03

Nadishan


1 Answers

Try to use .filter() for this purpose, value attribute won't get affected while you changing the value in it.

$('input:text').filter(function(){
   return $.trim(this.value).length == 0;
}).css("border","1px solid red");
like image 136
Rajaprabhu Aravindasamy Avatar answered Apr 02 '26 21:04

Rajaprabhu Aravindasamy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!