Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select all inputs, labels, selects etc within THIS - each loop

I'm working on some complicated form at the moment.

Just wondering, is there any better way to do this:

$('.selector').each( function(){

    $("input", this).prop('disabled', true);
    $("select", this).prop('disabled', true);
    $("label", this).prop('disabled', true);
    $("textarea", this).prop('disabled', true);

});

I want to select all inputs within this (currently looped through .selector). Am I doing this correctly?

like image 985
Iladarsda Avatar asked Nov 14 '11 15:11

Iladarsda


1 Answers

That's fine, but to simplify it you should be able to use the comma as you would to group any other selectors:

$('.selector').each(function() {
    $('input, select, label, textarea', this).prop('disabled', true);
});

If the only thing you're doing is setting that property on those elements, then you don't really need the .each() loop. You can safely drop that and reduce it to this one-liner:

$('input, select, label, textarea', '.selector').prop('disabled', true);
like image 137
BoltClock Avatar answered Oct 14 '22 17:10

BoltClock