YogeshChauhan . com

How to change style of all direct children of an element using jQuery?

in jQuery on March 7, 2021

We can use the children() method to get all direct children of the specified element.

The .children() method searches through the children elements in the DOM and constructs a new jQuery object from the elements that are matched.

The .children() method only travels a single level in the DOM tree.

If you want to traverse multiple levels and select all descendant elements (we call them grandchildren!) in the DOM tree then use the .find() method.

The .children() method doesn’t return text nodes.

We need use the .contents() method to get all children including text and comment nodes.

We can also optionally use selectors to filter out the elements. We can pass selectors using the $() function.

A quick example of jQuery .children() method

This example selects all direct children of ul with level-2 class.

Change style for all direct elements with a class using jQuery .children() method

Credit: jQuery Docs

amazon

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

The SQL UNION OperatorSQL/MySQLHow to make a web page look good on any device?HTMLWordPress: How to display slider repeater fields in ACF?WordPressThe SELECT DISTINCT Statement in SQLSQL/MySQLCreate a simple bar chart and column chart using pure CSS gridCSSHow to Block IPs and User Agents using code in Drupal or WordPress?DrupalContent types in DrupalDrupalUNION and UNION ALL in PostgresPostgresAn Example of Cross-site Scripting (XSS) Attack in PHP and How to Avoid It?PHPPHP arrays for US states’ full names and abbreviationsPHPHow to check if checkbox is checked or not using JavaScript?JavaScriptHow to get Current Hour, Minute and Second in JavaScript?JavaScript