Answer the question
In order to leave comments, you need to log in
How to change css file with JS and return original value?
Good afternoon!
I'm just starting to learn JavaScript and I have a question. It is necessary to change the href attribute in the link tag on button click to replace the css file.
Wrote a function:
function changeCss() {
document.getElementById("styleBright").href="css/style.css";
}
Answer the question
In order to leave comments, you need to log in
Well, take the current value of the attribute and, depending on it, do what you need.
var link = document.getElementById("styleBright"),
href = link.getAttribute('href');
if(href == 'css/style.css') {
// do something
} else if (href == 'css/style2.css') {
// do something
}
Maybe I'm wrong, but maybe this:
if (document.getElementById("styleBright").href==="css/style.css") {
//что-то делаем
} else {
//чтото делаем, например:
document.getElementById("styleBright").href="css/style.css";
}
Thanks to all! I went the other way:
function changeCss() {
document.getElementById("styleBright").href="css/style.css";
document.getElementById("switcher").value="Back to default";
document.getElementById("switcher").onclick = changeCssToDefault;
}
function changeCssToDefault() {
document.getElementById("styleBright").href="css/main.css";
document.getElementById("switcher").value="Switch the light";
document.getElementById("switcher").onclick = changeCss;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question