• Skip to primary navigation
  • Skip to main content
Matthew Dunbar

Matthew Dunbar

Hire Me
  • About
  • Portfolio
  • Blog
    • General
    • HTML
    • jQuery
    • PHP
    • Optimization
  • Hire Me
You are here: Home / Archives for jQuery

jQuery

How to Add a Class to a Child Element Using jQuery

February 5, 2017

The Description

One of the best features that jQuery offers is to simply add classes to elements. There’s times we’ll run into child elements with no classes and we’re unable to edit their template file. In this case, we’ll use jQuery to add a class.

The Original


<div class="parent-element">
	<div></div>
	<div></div>
	<div></div>
</div>

The jQuery



<script>
$(document).ready(function () {
	$('.parent-element').children('div').addClass('child-of-parent-element');
});
</script>

The Result


<div class="parent-element">
	<div class="child-of-parent-element"></div>
	<div class="child-of-parent-element"></div>
	<div class="child-of-parent-element"></div>
</div>


Filed Under: jQuery

  • « Go to Previous Page
  • Page 1
  • Interim pages omitted …
  • Page 3
  • Page 4
  • Page 5
  • Page 6
  • Page 7
  • Page 8
  • Go to Next Page »
© 2025 Matthew Dunbar - All Rights Reserved Back to Top