Select all inputs, tags, select, etc. Inside THIS - every cycle

I am currently working on a complex form.

Just wondering if there is a 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 of this (currently looping through .selector ). Am I doing it right?

+8
javascript jquery html jquery-selectors
source share
1 answer

This is good, but to simplify it, you should be able to use a comma, as well as to group any other selectors:

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

If the only thing you do is set this property for these elements, then you really don't need a .each() loop. You can safely discard this and reduce it to this single-line layer:

 $('input, select, label, textarea', '.selector').prop('disabled', true); 
+16
source share

All Articles