How would I change the color of the tag
I’m trying to make a dark mode bookmarklet to cover all of the html elements change
document.body.style.background = 'black';
This changes the color of the body so what would I do for
I tried
document.body.style.background = 'black';
document.body.style.color = 'white';
document.div.style.background = 'black';
document.div.style.color = 'white';
but I cant seem to get change the color of the div
4
Answers
if the
div
has anid
, you can do this:First you need to select the elements, then you can apply styling on that. Here is a an example snippet:-
I used querySelector() to select the elements in the above example. There are many articles that can explain how to select the elements from DOM.
A better solution would be to apply a separate class for dark mode:
You can’t access div elements this way "document.div".
You must select the element(s) you want to modify.
If you want to modify a single element you must select it with document.getElementById("id")
or document.querySelector("") select by "#id" or class ".className" or tag "tagName".
To modify several elements you must select them all:
document.querySelectorAll()
You can see all the examples below.