Forum Moderators: open

Message Too Old, No Replies

Javascript with an error message

         

toplisek

2:46 pm on Jun 26, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I try to fix Javascript error:
How to manage this as it is reported missed ; character.
var checkbox = document.querySelector('input[name=theme]');


checkbox.addEventListener('change', function()
{
if(this.checked) {
trans()
document.documentElement.setAttribute('data-theme', 'dark')
} else {
trans()
document.documentElement.setAttribute('data-theme', 'light')
}
}
)

let trans = () => {
document.documentElement.classList.add('transition');
window.setTimeout(() =>
{
document.documentElement.classList.remove('transition')
}, 1000
)
}


[edited by: not2easy at 5:45 pm (utc) on Jun 27, 2020]
[edit reason] Added CODE tags/readability [/edit]

NickMNS

6:51 pm on Jun 26, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What is the error exactly?

If it is that you are missing a semicolon ( ; ), then its simple. There should be a semicolon after each line of the if statement.


checkbox.addEventListener('change', function() {
if(this.checked) {
trans();
document.documentElement.setAttribute('data-theme', 'dark');
} else {
trans();
document.documentElement.setAttribute('data-theme', 'light');
}
});




[edited by: not2easy at 5:51 pm (utc) on Jun 27, 2020]
[edit reason] spaced smilies/readability [/edit]

toplisek

11:59 am on Jun 27, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, but at the bottom it is missing ; before statement. How to solve also the whole code?
let trans = () => {
document.documentElement.classList.add('transition');
window.setTimeout(() =>
{
document.documentElement.classList.remove('transition')
}, 1000
)
}



[edited by: not2easy at 5:46 pm (utc) on Jun 27, 2020]
[edit reason] Added CODE tags/readability [/edit]

NickMNS

5:29 pm on Jun 27, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I don't see what the problem is. It tells you where it is missing. If you show the indentation it would be more obvious. Aslo use {code}{/code} but with square brackets [] to post code snippets. it makes it easier to read


let trans = () => {
document.documentElement.classList.add('transition');
window.setTimeout(() => {
document.documentElement.classList.remove('transition')
}, 1000); // semicolon was missing here
}

toplisek

11:22 am on Jun 28, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thank you for the message. I will be more careful with the CODE Tag.