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 7946847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:15:56+00:00 2026-06-04T01:15:56+00:00

I’d like to dynamically set the background color of a div equal to the

  • 0

I’d like to dynamically set the background color of a div equal to the background color of the table row that directly proceeded it. Here’s what I think I want to do:

$(this).next().css('background-color', $(this).css('background-color'));

When looking for an answer on this board, I found this thread. This seems to be the answer that worked for them, but mine doesn’t work. Any suggestions gratefully accepted.

EDIT

For those who have asked to see what I’m trying to do, here is the PHP code that is building the body of my table:

<?php
//  For every row past the first (header) row, create a table entry
    while ( $row = fgetcsv($handle) ) {

        echo "<tr class='c_edd_row'>";                      
        //  Build each cell         
        $num = count($row);         
        for ($c=0; $c < $num; $c++) {
            //  Do not echo entries 23 through 27 (unused cells)
           if ( $c < 23 || $c > 27 ) {
              echo "<td>".$row[$c]."</td>";
              //  save the last entry for the hidden div
              $validation_notes = $row[$c];
          }
        }
        echo "</tr>";
        echo "<tr class='c_sub_row'>
        <td colspan='42' class='c_notes_div'>
        <div class='c_notes_div'>".$validation_notes."</div>
        </td></tr>";
    }
?>

Responders were correct, I didn’t state my problem very well. The divs I was talking about are actually contained in TRs. I’m using this as a dropdown for every row with data in the last row entry.

When I click a row, the following row with class c_sub_row changes from display:none to display:table-row, which works great, but I need it to take the color of the proceeding row when that happens.

FINAL UPDATE – Big thanks to Jonathan Sampson

Here is the final solution, based on the comments below:

$('.c_edd_row').on('click',  function(event) {
    var bg = $(this).css("background-color");
    $(this).next().css("background-color", bg);   //  colors the next row
    $(this).next().find('div').css("background-color", bg);  //  colors the div
});
  • 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-04T01:15:57+00:00Added an answer on June 4, 2026 at 1:15 am

    Table rows never directly precede div elements – that would be invalid table markup. If you wanted to set the background of a tr to that of the tr that preceded it, you could do the following:

    $("tr:eq(1)").css("background-color", function(){
      return $(this).prev().css("background-color");
    });
    

    Or perhaps you have a div within a tr, and you want the div to have the same background color as the tr that precedes the parent of the div:

    $(this).css("background-color", function(){
      return $(this).closest("tr").prev().css("background-color");
    });
    

    Where this is the current div element.

    The magic here happens in the anonymous function used as the second argument to the $.css method. From within it you can do just about anything. For instance, we could pick out any other element on the page and copy its background color:

    $(this).css("background-color", function(){
      return $(".someOtherDiv").css("background-color");
    });
    

    Again, assuming this is our target div element, it will now have the same background color as <div class="someOtherDiv">...</div>.

    Update

    From the comments, it sounds like you have two table rows (at least). One visible, and the next one hidden. Within the second one, you have a div. When you click the first (visible) table row, you want to both show the second and apply the bg of the first TR to the DIV within the second (now visible) table-row:

    $("tr:visible").on("click", function(event){
      var bg = $(this).css("background-color");
      $(this).next().show().find("div").css("background-color", bg);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have two tables with like below codes: Table: Accounts id | username |
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I would like to run a str_replace or preg_replace which looks for certain words

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.