• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / HTML / How Do You Select All p Elements Inside a Div Element?

How Do You Select All p Elements Inside a Div Element?

October 9, 2022 Leave a Comment

To select all p elements inside a div element, we can do this using JavaScript, jQuery, or CSS. Let’s take a look at how to do this in each.

Let’s first look at how to select all p elements inside a div element using JavaScript.

We will have to use the getElementById() method along with the getElementsByTagName() method. This will get us a list of all of the p elements within the div. We then have to loop through each one to access each p element.

var pTags = document.getElementById("div1").getElementsByTagName('p');
for( var i=0; i<pTags.length; i++ ){
  pTags[i]
  //Do something to each p element
}

Let’s take a look at this with an example.

<div id="div1">
  <p>This is a paragraph in a div element div1.</p>
  <p>This is a paragraph in a div element div1.</p>
  <p>This is a paragraph in a div element div1.</p>
</div>
<div id="div2">
  <p>This is a paragraph in a div element div2.</p>
  <p>This is a paragraph in a div element div2.</p>
  <p>This is a paragraph in a div element div2.</p>
</div>

Using our JavaScript code from above, we can select all p elements inside the div, #div1 and change the background of each one. Let’s see the code above first without any JavaScript code affecting it.

This is a paragraph in a div element div1.

This is a paragraph in a div element div1.

This is a paragraph in a div element div1.

This is a paragraph in a div element div2.

This is a paragraph in a div element div2.

This is a paragraph in a div element div2.

And now let’s use the following JavaScript code to change the background color of each p element in #div1 to green.

var pTags = document.getElementById("div1").getElementsByTagName('p');
for( var i=0; i<pTags.length; i++ ){
  pTags[i].style.backgroundColor = "green";
}

Which would result in the following:

This is a paragraph in a div element div1.

This is a paragraph in a div element div1.

This is a paragraph in a div element div1.

This is a paragraph in a div element div2.

This is a paragraph in a div element div2.

This is a paragraph in a div element div2.

This can be done with much less code by using jQuery. Let’s see how this is done below.

How To Select All p Elements Inside a Div Element Using jQuery

We can use jQuery to select all p elements inside a div element easily. Here is the code to do this.

$("#div1 p")

And that’s it.

Let’s see this in action with our same example from above.

<div id="div1">
  <p>This is a paragraph in a div element div1.</p>
  <p>This is a paragraph in a div element div1.</p>
  <p>This is a paragraph in a div element div1.</p>
</div>
<div id="div2">
  <p>This is a paragraph in a div element div2.</p>
  <p>This is a paragraph in a div element div2.</p>
  <p>This is a paragraph in a div element div2.</p>
</div>

And here is the jQuery code to change the background colors of all p elements in #div1 to green.

$("#div1 p").css("background-color","green");

Which would result in the following:

This is a paragraph in a div element div1.

This is a paragraph in a div element div1.

This is a paragraph in a div element div1.

This is a paragraph in a div element div2.

This is a paragraph in a div element div2.

This is a paragraph in a div element div2.

Finally, let’s see this done using CSS.

How To Select All p Elements Inside a Div Element Using CSS

We can use CSS to select all p elements inside a div element easily. Here is the code to do this.

<style>#div1 p { 
  //Do something to each p in div1 
}</style>

Let’s see this in action with our same example from above.

<style>#div1 p { background-color: green; }</style>
<div id="div1">
  <p>This is a paragraph in a div element div1.</p>
  <p>This is a paragraph in a div element div1.</p>
  <p>This is a paragraph in a div element div1.</p>
</div>
<div id="div2">
  <p>This is a paragraph in a div element div2.</p>
  <p>This is a paragraph in a div element div2.</p>
  <p>This is a paragraph in a div element div2.</p>
</div>

Which would result in the following once more:

This is a paragraph in a div element div1.

This is a paragraph in a div element div1.

This is a paragraph in a div element div1.

This is a paragraph in a div element div2.

This is a paragraph in a div element div2.

This is a paragraph in a div element div2.

Hopefully this article has been useful to help you understand how to select all p elements inside a div element.

Other Articles You'll Also Like:

  • 1.  HTML superscript – How to Raise Text in HTML
  • 2.  How to Call a JavaScript Function From HTML
  • 3.  innerHTML vs outerHTML
  • 4.  How to Make a Div Scrollable in HTML
  • 5.  Clicking on an Image to Enlarge it in HTML
  • 6.  What is the Correct HTML for Inserting a Background Image?
  • 7.  What is the Correct HTML for Making a Drop-Down List?
  • 8.  What Type of HTML List will Automatically Place a Number in Front of the Items?
  • 9.  HTML span width – Set the Width of a Span in HTML
  • 10.  How to Filter a List of Divs with a Text Input Bar Using Javascript

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy

x