Sathish
Sathish

Reputation: 11

How to disable empty textboxes in a page using JQuery

I have a page with many panels. each panel will have around 5 textboxes. I need to disable all the textboxes which are empty when the page is loaded. Want to acieve this using JQuery. Can somebody help me on this?

Upvotes: 1

Views: 838

Answers (2)

user113716
user113716

Reputation: 322502

var empties = $('input:text').filter(function() {
    return this.value == '';   // If the value of the input is empty, 
});                            //    add it to the collection

empties.attr('disabled','disabled');   // Then disable the collection of empties

Upvotes: 1

Sarfraz
Sarfraz

Reputation: 382726

This should do it:

$(function(){
 $('input[type="text"]').each(function(){
   if ($(this).val() === '') {
     $(this).attr('disabled', 'disabled');
   }
 });
});

If you have applied some class to your textboxes, you can also do:

$(function(){
 $('.class_name').each(function(){
   if ($(this).val() === '') {
     $(this).attr('disabled', 'disabled');
   }
 });
});

Upvotes: 3

Related Questions