/    Sign up×
Community /Pin to ProfileBookmark

Smarter way to do this in Javascript?

I am trying to apply themes. I did it get to work, but the javascript looks a bit newbie to me.

https://jsfiddle.net/dLoz9jk5/15/

Is there smarter ways to achieve this?

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@sibertauthorFeb 21.2020 — Found one way to make it cleaner:

https://jsfiddle.net/8co10z6r/6/

But other suggestions are still welcome...
Copy linkTweet thisAlerts:
@SempervivumFeb 21.2020 — That's fine! Additionally you might use dataset:

https://jsfiddle.net/Sempervivum/103Lqctn/20/
Copy linkTweet thisAlerts:
@sibertauthorFeb 21.2020 — Brilliant! Thank you!

But I wonder what "idx" do? Necessary?

document.querySelectorAll('.btn-theme').forEach((item, idx)
Copy linkTweet thisAlerts:
@SempervivumFeb 21.2020 — idx is standard in forEach, but as in this context it is not needed one can omit it.
Copy linkTweet thisAlerts:
@sibertauthorFeb 22.2020 — I am trying to set this value in localStorage and use it at startup, but it fails.

How do I use this settings like <html data-theme=localStorage.theme>?

https://jsfiddle.net/bgqmLnvj/4/

another approach with onload does not work either

https://jsfiddle.net/bgqmLnvj/8/

Or how do I USE this on every page?
Copy linkTweet thisAlerts:
@SempervivumFeb 22.2020 — Your first fiddle works fine when you read the theme from the local storage and apply it to document.body:
document.querySelectorAll('.btn-theme').forEach((item) =&gt; {
item.addEventListener('click', (event) =&gt; {
window.localStorage.setItem('theme', item.dataset.theme);
alert(localStorage.theme + " is stored")
});
});
document.body.dataset.theme = localStorage.getItem('theme');


In your second fiddle you are using the onload event. Unfortunately jsfiddle (by default) does the same, it calls the javascript onload. Therefore that event has already fired when your javascript is executed and won't fire again. You can view and adjust the load type, i. e. the event when the javascript is executed, by the JS setting: The small down triangle on top of the javascript window.
Copy linkTweet thisAlerts:
@sibertauthorFeb 23.2020 — > @Sempervivum#1615109 In your second fiddle you are using the onload event. Unfortunately jsfiddle (by default) does the same, it calls the javascript onload.

You are correct: Some settings fixed this:

https://jsfiddle.net/k28b7jxf/6/

I managed also to do this "live".

http://94.237.92.101:2020/myprefs

Thank you again!
×

Success!

Help @sibert spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...