Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

186
Views
Recursively disabling all children of an element

This might be a yes/no type of question.

I'm trying to disable absolutely all children of an element in jquery.

Does calling

$('#id_of_an_element').children().do(function(){
    do_something;
});

recursively call all children of an element, or does it just do_something to all the direct descendants of an_element?

Help is appreciated,

Josh

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Given a jQuery object that represents a set of DOM elements, the .children() method allows us to search through the immediate children of these elements in the DOM tree and construct a new jQuery object from the matching elements. The .find() and .children() methods are similar, except that the latter only travels a single level down the DOM tree. Note also that like most jQuery methods, .children() does not return text nodes; to get all children including text and comment nodes, use .contents().

http://api.jquery.com/children/

You can do this if you want to act on all descendants at any level of nesting:

$('#id_of_an_element').find('*').attr('disabled', true);

or using the descendant selector:

$('#id_of_an_element *').attr('disabled', true);
over 4 years ago · Santiago Trujillo Report

0

Since you want to affect all descendants, just do this:

$('#id_of_an_element *').each(function() {
    // do something
});

But I'd be curious to know what exactly you're doing.

The disabled property is meaningless for many element types. It could be that whatever you're doing will benefit from the inheritance of CSS.

Or if you actually want the disabled property, then you might as well just target form elements.

$('#id_of_an_element :input').attr('disabled','disabled');
over 4 years ago · Santiago Trujillo Report

0

You can do

function disableChildren(obj) {
    obj.children().each(function(i, val) {
         disable(val);
         disableChildren(val);
    });
}

disableChildren($("#id"));

See .each There doesn't appear to be a .do method.

You'll have to implement disable as a function to do what every you want when you say "disable"

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!