var node = document.querySelector('[title="40% Discount!"]').style.color = "red";
<div title="40% Discount!">40% off</div>
var node = document.querySelector('[title="40% Discount!"]').style.color = "red";
<div title="40% Discount!">40% off</div>
You can set the style properties of an existing element:
const node = document.querySelector('[title="40% Discount!"]')
node.style.color = "red";
In modern Webdevelopment you should not use .style
in JS as it adds it as inline-style and as such has the highest specificity weight that only get overwritten by !important
. The modern solution should be to add a class.
Use classList.add('class-name')
to add a class to that element that contain color: red;
instead!
var node = document.querySelector('[title="40% Discount!"]').classList.add('red');
.red {
color: red;
}
<div title="40% Discount!">40% off</div>
I selected an element by title. From now on I want to update the css of this text. For ex. ‘color: red;’ how should I move on?
var node = document.querySelector('[title="40% Discount!"]');
you can use ` style ` property ` node.style.color=red `
Edited. Watch out, I think my original post mixed up jquery and js.
Thanks alot wazz, that worked perfect!
You should however not use
This one was very usefull for me. Adding class to locked html is saved my day thanks alot Tacoshy!
If that solves the issue for you, then please consider to up-vote it and mark it as an answer.