Problem with selecting odd and even div elements

I have a div populated with a list of rows, and I want to alternate the row colors. To achieve this, I am using the following code:

$('#PlatformErrorsTableData').html(table1Html);
$('#PlatformErrorsTableData div:nth-child(even)').css("background-color", "aqua");

The structure of table1Html is as follows:

<div>
  <span class="platformTable columnWidth10">Originator1</span>
  <span class="platformTable truncate">Message for Originator# 1</span>
  <span class="platformTable columnWidth15">Server1</span>
  <span class="platformTable columnWidth20">5:50 PM</span>
</div>
<div>
  <span class="platformTable columnWidth10">Originator2</span>
  <span class="platformTable truncate">Message for Originator# 2</span>
  <span class="platformTable columnWidth15">Server2</span>
  <span class="platformTable columnWidth20">5:50 PM</span>
</div> 
<div>
  <span class="platformTable columnWidth10">Originator3</span>
  <span class="platformTable truncate">Message for Originator# 3</span>
  <span class="platformTable columnWidth15">Server3</span>
  <span class="platformTable columnWidth20">5:50 PM</span>
</div>
<div>
  <span class="platformTable columnWidth10">Originator4</span>
  <span class="platformTable truncate">Message for Originator# 4</span>
  <span class="platformTable columnWidth15">Server4</span>
  <span class="platformTable columnWidth20">5:50 PM</span>
</div>

However, despite setting the background color to aqua, it is not displaying on screen. Upon inspecting the div with Firebug, the background color property is correctly set to aqua.

Any assistance in resolving this issue would be greatly appreciated.

Answer №1

Try implementing CSS code rather than jQuery

#ErrorTableData div:nth-child(2n){
   background:#a5a5a5;
}

#ErrorTableData div:nth-child(2n+1){
   background:#eee;
}

Answer №2

Your code is functioning perfectly on this end.

Feel free to check out the Live Demo.

If you're experiencing issues with your CSS styling, consider adding overflow:hidden and setting a min-height for your div element.

This adjustment might resolve any potential problems you're facing.

Answer №3

Consider specifying a specific height for the div elements initially. If this solves the issue, it is likely related to CSS problems and you might need to add something like overflow: hidden; to them (after removing the height). Additionally, ensure that the span elements have a background color set to transparent.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Performing multiple http requests in a loop and combining the retrieved data using AngularJS

I am currently working with a piece of source code that looks like this: var projectPromises = $http.get('http://myapi.com/api/v3/projects?private_token=abcde123456'); $q.all([projectPromises]).then(function(data) { console.log(data); ...

The python-pyinstrument is in need of a javascript dependency that seems to

As I attempt to profile my Python program using pyinstrument, I encounter an error when trying to view the profile in HTML format. Traceback (most recent call last): File "/home/ananda/projects/product_pred/025200812_cpall_ai_ordering_model_v2/.venv ...

Bootstrap Card breaking out from the confines of its parent container

Attempting to replicate Bootstrap's Cards example in my project, I noticed that the top margin of each Card extends beyond the parent element, a border div. If you need more information on how to utilize Card, refer to Bootstrap's documentation ...

jquery logic for iterating through all elements in a select menu encountering issues

In search of a solution to iterate through all options in a dropdown list using code, comparing each to a variable. When a match is found, I aim to set that particular value as the selected item in the dropdown and then exit the loop. Here's what I&ap ...

Retrieve the item within the nested array that is contained within the outer object

I have a collection of objects, each containing nested arrays. My goal is to access the specific object inside one of those arrays. How can I achieve this? For instance, take a look at my function below where I currently log each array to the console. Wha ...

Choose from the select2 multiselect options and lock certain selected choices from being altered

I have coded a select dropdown with preselected options, some of which I want to keep fixed so that users cannot change them. To achieve this, I implemented the following code: <select id="select2-multiple" name="users" multiple="multiple" style="width ...

Using feature flags in Vue for enhanced functionality

I am interested in incorporating a "feature toggling mechanism" into my Vue application. Although I have set up a basic system, I want to explore the methods outlined in a article by Pete Hodgson. The concept of "Inversion of Decision" seems particularly i ...

Is it possible to include Helvetica or a similar font in my web application for shipping?

My web-app is Java/HTML based and hosted on the cloud. Users will access it using IE, Chrome or Mozilla. I would like to use Helvetica or a similar font, but they are not readily available on most systems (windows/IE/Chrome/Mozilla). Is there a way for me ...

Nested pages are causing jQuery plugins to malfunction

I am currently working on a website, but I am facing some issues with the product listing pages and the tips and tricks page. It appears that there is an issue with jMenu and jFlipBook plugins not functioning properly. Since I didn't develop the origi ...

Guidelines for validating email input using jQuery

Although I am not utilizing the form tag, you can still achieve form functionality using jQuery Ajax. <input type="email" placeholder="Email" name="email" /> <input type="password" placeholder="Password ...

Utilizing jQuery to submit the form

After clicking the search icon, it displays an alert with the message ok, but not h. Based on the code snippet below, it is intended to display alerts for both ok and h. <?php if(isset($_POST['search'])){ echo "<script type='text/ ...

The collapsible menu located beneath my navigation bar is unable to reach the correct position on the right side

I'm attempting to create a collapsible navigation bar with its contents shifting to the right side when resized to medium size, but I'm having trouble. Can someone please assist me with this? I have also included the code below. <nav class=&qu ...

How can I make a single <input> element that can handle both files and urls simultaneously?

Is there a way to enable my users to import both local and remote files using just one input field? A possible solution, inspired by Stackoverflow, is to implement tabs: Another approach could be using radio buttons like this: <style> .display ...

Utilize Javascript to convert centimeters into inches

Can anyone help me with a JavaScript function that accurately converts CM to IN? I've been using the code below: function toFeet(n) { var realFeet = ((n*0.393700) / 12); var feet = Math.floor(realFeet); var inches = Math.round(10*((realFeet ...

What is the best way to run a function within an if statement without duplicating code if the condition is false?

When working with relay mutation code, I find it necessary to reload the store in order for it to sync with the database. This is because if the text being added is the same as previously added text, the relay store throws an error called flattenChildren.. ...

Tips for Resolving React.js Issues with setInterval

Is there a way to adjust setInterval in React so that it doesn't count faster than it should at the beginning of my program? Currently, 'var i' is being increased by 2 every second. How can I modify my code to resolve this issue? import Reac ...

Maximizing PUT Methods in HTTP RESTful Services

I've been playing around with my routes file and I'm looking to switch up the method being called (delete instead of update). Code Snippets: # User management API GET /users @controllers.Users.findUsers POST /user ...

Dividing the ZMQ socket from the express app.js by integrating with mongodb

Currently, I am working on an Express application that is responsible for gathering data sent from a remote machine through ZMQ and then updating a MongoDB database with this information. The data updates are transmitted every 5 minutes in the form of enc ...

Moving Cursor Automatically to the End Upon Rejecting Input

I have a form input where users can enter values. I need to validate the inputs to ensure they only contain numbers, dots, and commas. If the input is invalid, I want to prevent it from being stored in the input field. To achieve this, I have implemented ...

Enable vertical scrolling on the second row of a table while keeping the first row fixed as the table header (CSS)

While embedding the Ace Editor in a Chrome popup window, I encountered a challenge. My goal was to keep the first row of text always visible at the top while allowing the rest of the Editor to fill the screen. However, there is an issue with a vertical scr ...