Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling all input elements except some in a form using jQuery

I am trying to disable almost all input elements in a form using jquery, but I need some input elements to be enabled. For example:

$(document).ready(function () {
    $("#document :input[name!='tloEnable']).attr("disabled", true);
});

This works great on the elements I have with the same name 'tloEnable'. However there are a few other elements that have different name attributes (filename, notifyUsers, notifyTeam). How can I include them too while disabling the remaining input elements?

$(document).ready(function () {
    $("#document :input[name!='tloEnable], [name!='filename'], [name!='notifyUsers'], [name!='notifyTeam']).attr("disabled", true);
});
like image 962
Jason Avatar asked Dec 01 '22 18:12

Jason


2 Answers

Use the .not() function and pass a selector; matched elements will be excluded:

$(document).ready(function () {
    $(":input").not("[name=tloEnable], [name=filename], [name=notifyUsers]")
        .prop("disabled", true);
});

The :not() selector works the same way:

$(document).ready(function () {
    $(":input:not([name=tloEnable], [name=filename], [name=notifyUsers])")
        .prop("disabled", true);
});
like image 92
Salman A Avatar answered Dec 04 '22 09:12

Salman A


Give the inputs you wish to disable a class such as disabled-inputs. Then simply:

Jquery 1.6:

$(".disabled-inputs").prop('disabled', true);  

jQuery 1.5 and below:

$(".disabled-inputs").attr('disabled','disabled');
like image 41
Carl Reid Avatar answered Dec 04 '22 09:12

Carl Reid