X

LATEST #JQUERY NEWS

jQuery’s .children() Method


Just like in CSS, it’s possible to use jQuery to select the children of an element and apply jQuery to them. This can be done using jQuery’s .children() method. The method works pretty much like any other jQuery method — to select all of the children of an element, use the parent element as the selector and apply the .children() method to it:

$(“.main”).children();

The example above simply selects all of the children of the .main class element(s). To apply jQuery to those elements, you’ll need to do some method chaining like in the example below:

$(“.main”).children().css(“color”, “#000”);

In the example above, we’re not only selection all the children of the .main class, but we’re also adding CSS rules to them (in this case, the rule is to make all of the text black).

The .children() method does accept a filter parameter, which you can use to select specific children of a certain element. To use this feature, you’ll need to pass the element type or the element’s class or ID through the parameters of the .children() method to be sure that only children of this element type, class, or ID are selected.

$(“.main”).children(a).css(“text-decoration”, “underline”);

The example above uses the filter parameter to only select the children of the .main element(s) that are also a elements, and then applies a CSS rule to those a elements.

Recently Published

Screen Shot 2017-04-23 at 10.15.41 AM
»

How to Use jQuery to Create a Sticky Navigation on Scroll

Creating “sticky” elements is something that can often be ...

article image
»

How to Use jQuery’s Keystroke Methods

jQuery has a few built-in keystroke methods that are all triggered by ...

article image
»

How to Change iFrame Styling Using jQuery

If you’ve ever had the displeasure of working with iFrames, ...

article image
»

How to Use jQuery to Create Blinking Text

Blinking text is one of those cool features that you never actively ...

article image
»

Convert Images to Black and White using jQuery

Converting an image to black and white is no longer a manual event. ...

article image
»

How to Disable Right-Click Menu Using jQuery

There can be dozens of reasons why you may want to disable right ...

article image
»

How to Optimize jQuery Code for Better Performance

jQuery is the most popular client side library, in fact almost every ...

article image
»

Using Head.js to Load All Your JavaScript

In the past few years, client side frameworks/libraries have become ...

article image
»

How to Detect Browser Using jQuery

One of the more frustrating parts of creating a new web project is ...