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

article image
»

Changing images in a loop on hover using jQuery

Why use this Function? Changing images in a loop on mouse over is a ...

article image
»

How to Dynamically Trim Text Using jQuery

This easy beginners jQuery tutorial demonstrates how you can use ...

article image
»

jQuery Snippets: Getting the Current Year

Here’s a fun, simple, and incredibly useful jQuery that you can ...

article image
»

jQuery Snippets: How to Conditionally Enable Submit

One easy way to prevent users from submitting an empty form in a ...

article image
»

How to Use Google Analytics with jQuery Mobile

jQuery Mobile is an HTML5 based web-page framework that’s ...

article image
»

jQuery Snippets: Using jQuery to Check All Boxes

When you’ve given your users a ton of checkbox input fields ...

article image
»

Useful jQuery Code Snippets for Working with Images

Images are an essential part of any website and sometimes working ...

article image
»

How to Use jQuery to Check if Cookies are Enabled

A cookie (not the kind with chocolate chips) is a small piece of data ...

article image
»

A Beginner’s Guide to jQuery’s Selectors

We talk a lot about event methods on this site, but not so much about ...