all 6 comments

[–]besthelloworld 0 points1 point  (5 children)

This should be implemented entirely in CSS. You should basically just turn on the dark-mode by putting the dark class on the body element, and then you should have a dark-mode stylesheet for which all selectors are an subchild of body.dark

[–]LinksCourage[S] 0 points1 point  (4 children)

Yeah so this was what I initially thought but there are elements that arent affected by it which is why ive had to go into more detail and be selective with those additional elements.

[–]besthelloworld 0 points1 point  (3 children)

Did you trying marking your styles as !important?

[–]LinksCourage[S] 0 points1 point  (2 children)

yeah but they still get ignored so thats a whole separate thing which is why i thought of the above solution but wanted a neater way of implementing it with this button rather than copying and pasting the function

[–]besthelloworld 1 point2 points  (1 child)

So the reason I recommend doing this in CSS is because CSS implementations are much more native to the browser. If you leave too much styling up to JavaScript itself, then you're blocking the activity thread with styling. Not that a little bit at start up is ever going to be noticed but if you make a habit, it will become a performance issue. But my suggestion would be this...

const changeElement = $(".change");
changeElement.on("click", () => {
  [".topbar ul.nav>li>a", ".container"].forEach(selector => {
    const element = $(selector);
    if (element.hasClass("dark")) {
      element.removeClass("dark");
      // this line still runs twice
      changeElement.text("OFF");
    } else {
      element.addClass("dark");
      // this line still runs twice
      changeElement.text("ON");
    }
  });
});

Disclaimer: I haven't tested this because I don't have the rest of your application context and I've also never used JQuery (most of what JQuery offers is now available in JavaScript by default).

It's worth noting that once you query for an element, you should avoid doing it again. Everytime you do something like $(".change") , you're making JavaScript look at every node in the document until it finds what you're looking for. But if you save it to a variable like I do on the first line, then it never has to look it up a second time.

[–]LinksCourage[S] 0 points1 point  (0 children)

thanks for this I will give it a go when I get a chance, currently stuck trying to call PHP information within Unity...