Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9199125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:27:28+00:00 2026-06-17T22:27:28+00:00

I have a for loop in PHP that creates four div , each containing

  • 0

I have a for loop in PHP that creates four div, each containing a img and p string, as below:

<div class="guest-tile-holder">             
    <img class="guest-tile" src="<?php echo($tile); ?>">
    <p><span><?php echo($fn.' '.$ln); ?></span></p>
</div>

In jQuery, I want to colour every fourth p black, i.e. only the p string the fourth box created, as below:

$(document).ready(function() {
    $('p:nth-child(4)').css('color', 'black');
});

What I would expect is that the PHP would pre-process, creating four div as above and then jQuery would colour black the p string in the fourth div. What is actually happening however is jQuery appears to be ignoring the PHP and colouring a p string that is another three p strings away in my HTML.

I was always led to believe that PHP would pre-process, therefore I don’t understand why jQuery appears to be running before the PHP has completed.

Can anybody tell me how I would get the jQuery to run after PHP has finished?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:27:30+00:00Added an answer on June 17, 2026 at 10:27 pm

    Your jQuery is correctly executed after your PHP code, it’s just that you’re not selecting the right p.

    p:nth-child(4) will select the paragraph which is the 4th element in the hierarchy. For example, the first p would be the 3rd element since it is preceded by a div and an img. What you’re actually looking for is something like p:nth-of-type(4) or .guest-tile-holder:nth-child(4) or even using jQuery’s custom :eq() selector:

    $('.guest-tile-holder:eq(3) p').css('color', 'black');
    

    (Since JavaScript arrays use zero-based indexing, the 4th element has index 3.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a loop that when it is called, creates a div with some
Let's say I have this loop in my header.php: <div id=top-20> <h2 class=top-20-title>TOP 20
I have a php loop that is echoing out geolocation values. How can I
In PHP I can use a foreach loop such that I have access to
I have a loop that creates a small number of forms on a page.
I have a php loop that includes a count using $counter++ , within the
I have a config.php that is included to each page. In config I create
I have this loop in PHP that echoes out a table of results, <table
i have php while loop, creates many classes, displaying info (user posts ect) and
I have written a PHP class that gets the headers from a .xls spreadsheet

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.