October 22, 2024
Chicago 12, Melborne City, USA
javascript

Changing classname of “


New to JS. I’m trying to change the class of this:

<div data-node-key="holders" class="ant-tabs-tab">

To this:

<div data-node-key="holders" class="ant-tabs-tab ant-tabs-tab-active">

But nothing seems to work since all the tutorials are for query selecting of id’s or classes.
I think this code I worked out should do the trick, but I get an error saying:

Uncaught TypeError: Cannot read properties of null (reading 'classList') at window.onload

Here’s my code:

window.onload = function()
{
        const myElement = document.querySelector('[data-node-key="holders"]')
        console.log(myElement)

        myElement.classList.remove('ant-tabs-tab');
        myElement.classList.add('ant-tabs-tab ant-tabs-tab-active');

        console.log(myElement)
}

I’m injecting this code through a ‘custom JS’ Chrome extension to modify a website if that’s the problem? I can’t link you the webpage, because you need to be logged in to see anything, so I’ve attached a screenshot if that helps.

Screenshot of the HTML

Like I said, I’m new to JS, but I imagine the code gets executed before the page fully loads? Well, I’ve tried everything I could find and still can’t get it to work.

Tried using document.querySelector to select the element and classList.add to add the new class.



< h3>You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video