I am having trouble getting the desired results with style and class overrides in D3.js

Can anyone explain why the styles of my class "bold-header" did not override on the first row <tr> of the table?

HTML:

<style>
    .bold-header{
        background-color:navy;
        color:white;
    }
</style>

<table border="1">
    <tr>
        <td>ID</td>
        <td>Name</td>
    </tr>
    <tr>
        <td>001</td>
        <td>John</td>
    </tr>
    <tr>
        <td>002</td>
        <td>Alex</td>
    </tr>
    <tr>         
        <td>003</td>
        <td>Maxwell</td>
    </tr>
</table>

Script:

d3.select("table").selectAll("td").style("background-color", "lightblue").style("width", "100px");

d3.select("table").select("tr").classed("bold-header", true);

I was hoping for this result: my expectation

but what I got was this: actual result

Answer №1

The reason the background color is not displaying correctly is because you need to apply the background color to the <td> elements in the first row. Currently, your code is not targeting the correct td's.

To fix this issue, make the following changes to the second line of your JavaScript code:

d3.select("table").select("tr").selectAll("td").classed("bold-header", true);

UPDATE

I have reviewed the problem and updated the code accordingly. Another reason why your code may not be working is that the light blue color is overriding the navy color. I recommend selecting the header td tags and body td tags carefully as shown in the corrected code below:

d3.select("table").selectAll("tr:not(:first-child)").style("background-color", "lightblue").style("width", "100px");

d3.select("table").select("tr").classed("bold-header", true);
 .bold-header{
        background-color:navy;
        color:white;
    }
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<table border="1">
    <tr>
        <td>ID</td>
        <td>Name</td>
    </tr>
    <tr>
        <td>001</td>
        <td>John</td>
    </tr>
    <tr>
        <td>002</td>
        <td>Alex</td>
    </tr>
    <tr>
        <td>003</td>
        <td>Maxwell</td>
    </tr>
</table>

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

Update the knockout values prior to submitting them to the server

Imagine having a ViewModel structured like this with prices ranging from 1 to 100... var Item = { price1: ko.observable(), price2: ko.observable(), price3: ko.observable(), ... ... price100: ko.observable(), name: ko.observable ...

The solution to enabling multiple inputs when multiple buttons are chosen

Below is a link to my jsfiddle application: http://jsfiddle.net/ybZvv/5/ Upon opening the jsfiddle, you will notice a top control panel with "Answer" buttons. Additionally, there are letter buttons, as well as "True" and "False" buttons. The functionali ...

Struggling to make the background color of the "col" element in Bootstrap span the full area without creating a scrollbar

I am in the process of designing a webpage layout using Bootstrap 3.3.7 On the left, I have a scrollable sidebar, and on the right, there is a simple element like an image. My goal is to have a background image and color on the right side that covers the ...

Leverage the Power of Two AngularJS Factories

Can I efficiently use two Factories in AngularJS by calling one from the other? Here's the Scenario: I have a Factory that returns an Array. This Factory is responsible for checking if the data to populate this Array already exists in local SQL Stor ...

Can anyone help me figure out the best way to test for observable errors in Angular2?

I'm currently working on creating unit test cases for my Angular2 components. Up until now, the test cases have been running smoothly. However, I've run into some issues with my asynchronous calls. For example, I have a method for creating a n ...

Merge shared attributes into JSON with the help of JavaScript

I am looking to include a shared property in my JSON Object. Below is an example of the JSON object: data = [ { COUNTRY: 'USA', POPULATION: 300, }, { COUNTRY: 'USA', POPULATION: 50, }, { COUNTRY: 'Cana ...

When JSON data is copied and displayed in Node.js or JavaScript, it appears as a string

I have written a code to copy one JSON file into another JSON file. Here is the code: function userData(){ fs.copyFile( path.join(process.cwd(), "user/access.json"), path.join(process.cwd(), "client/access.json"), ...

Is there a way to align elements in a table cell (td) where one element is aligned to the left and the other to the right?

Given the html code shown below: <tr> <td>Some text <a href="">Some link</a> <button>Some Button</button></td> </tr> I am looking to achieve a layout where elements are positioned in different alignments ...

Using Input.checked will generate a dynamic element that must be connected to an input field

I am currently facing an issue with a filter that dynamically creates li elements when an input is checked. The problem is that I cannot figure out how to clear the input when you click on the element. I am interested in finding a way to bind the input and ...

How to remove the black border on a material-ui button

I am currently working with the material-ui datepicker and buttons. Whenever I click on the datepicker icon to select a date or any of the buttons, I notice a black border appearing. How can I remove this black border from the design? https://i.sstatic.ne ...

Is there a way to convert my validation function into an AJAX method?

document.getElementById("button1").addEventListener("click", mouseOver1); function mouseOver1(){ document.getElementById("button1").style.color = "red"; } document.getElementById("button2").addEventListener("click", mouseOver); f ...

Adaptable background images with CSS styling

I'm attempting to adjust the sizing of my background image on the splash page to make it smaller. My goal is for the image to fill the entire section regardless of screen size. .splash{ background-image: url("../images/lanternboys_medium.jpg"); backg ...

Revamp local route variables in Express.js without the need for a page refresh

Currently, I am working on a project using node.js along with the express.js framework and EJS for template handling. Below is my routing code: app.get('/',function(req,res) { res.render('index', { matches: [], status_messag ...

What steps can I take to ensure that the information in my Cart remains consistent over time?

I recently built a NextJS application integrating the ShopifyBuy SDK. My implementation successfully fetches products from the store and displays them to users. Users can navigate to product pages and add items to their cart. However, I encountered an iss ...

Creating a Dynamic Slideshow Rotation

Seeking help with an HTML5/jQuery slideshow system... I need the images to rotate continuously without pausing. Currently, when I reach the last slide, it goes back to the first by changing the margin-left to 0. Ideally, I want it to loop indefinitely. A ...

Splitting the Django REST backend from the front end

I've been researching for hours, but I still can't find the information I need. This is a different kind of question. My Django REST backend is set up with a very simple REST API that includes only one Model: Model.py from django.db import mode ...

Implementing functions to deactivate and activate features in Summernote editor

For a while now, I have been using the summernote editor and it has fulfilled most of my needs. However, I recently encountered an issue where I needed to disable it (not destroy it) after a user action. After several unsuccessful attempts, I realized that ...

Is it possible to retrieve a secure XML file via an AJAX request?

Is it feasible to retrieve a secure XML file through an AJAX call? I currently have the following code that successfully retrieves the xml file: function loadXMLFile() { var filename = 'test.xml'; $.ajax({ type: "GET" ...

What are the best practices for managing methods in asynchronous programming with the use of node.js, Ajax, and MySQL?

I have a query regarding the handling of methods using Node.JS, ajax, and mysql in the provided code snippet. When the client enters the correct username and password, the server should return a "true" value to my front-end (index.html) and redirect to th ...

How can I center align my loader inside app-root in Angular2+?

I've successfully added a basic spinner to my <app-root> in the index.html file. This gives the appearance that something is happening behind the scenes while waiting for my app to fully load, rather than showing a blank white page. However, I& ...