jQuery: Find all elements that are children of a div element
jQuery core : Exercise-1 with Solution
Find all h1 elements that are children of a div element and apply a background to them.
Sample Data :
<body> <h1>abc</h1><br><div> <h1>div-1</h1> <h1>div-2</h1> </div> <h1>xyz</h1> </body>
Solution:
HTML Code :
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<meta charset="utf-8">
<title>Find all h1 elements that are children of a div element and apply a background to them</title>
</head>
<body>
<h1>abc</h1>
<div>
<h1>div-1</h1>
<h1>div-2</h1>
</div>
<h1>xyz</h1>
</body>
</html>
JavaScript Code :
$( "div > h1" ).css("background", "#0686c9");
Live Demo:
See the Pen jquery-core-exercise-1 by w3resource (@w3resource) on CodePen.
Contribute your code and comments through Disqus.
Previous: jQuery Core Exercises
Next: Set the background color of a page to red.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics