tip pit
tip pit

Reputation: 21

Add a CSS attribute with JavaScript using the cssText function

I managed to insert an attribute in the html tag, using this code in JavaScript:

document.documentElement.style.cssText = 'cursor: url("https://image0.png"), auto !important;';

Therefore, the result in CSS was:

html {
    cursor: url("https://image0.png"), auto !important;
}

Now, I know this is probably a silly question, but I can't find the right JavaScript code to obtain this CSS result:

a {
    cursor: url("https://image1.png"), auto !important;
}

.content img {
    cursor: url("https://image2.png"), auto !important;
}

Upvotes: 0

Views: 390

Answers (1)

chrwahl
chrwahl

Reputation: 13110

As I read your question you would like to dynamically add CSS styles to your document using JavaScript.

You can add one or more style elements to the document.body. In this example it is just a string of CSS. An alternative could be to add a link element to the document.head.

var styleELm = document.createElement('style');
styleELm.innerText = "p {color: Steelblue} a {text-decoration: none}";
document.body.appendChild(styleELm);
<p>Hello <a href="#">World</a></p>

Upvotes: 1

Related Questions