10 Replies
This is just the switch case code within a bigger code that loop over an array of svg files and every 2 seconds replaces the svg shown
What I don't like about this switch case statement is that, I need to remove the the previously added class in the next switch case
It would be great if there was a way to have a switch case that ran for all cases, that way I can just have in there all the
remove
functions once
is there a way to achieve that?can you show us some more code ? (Make a small example in codepen) . Because i have the feeling this could be way easier (even in css)
From that switch statement it looks like the cases aren't removing all of the classes that could be present. eg the
python
one only removes javascript
, but there could be css
or html
etc.
First investigate doing this with CSS as mark suggested. CSS is way faster than JS, so if you can get something done there, do it there.
If JS is the only way though;
You could make a separate removeUnwantedClasses
function, something like;
Then in your existing code;
I updated the original post to include all the code from the file.js
I should have included all the code to begin with, but all the classes remove the one before it so all classes are getting removed
// html snippet of the inline svg
Still feels a bit too much but I think that's the smallest I can get the code to be. It would be amazing if I can somehow do this in CSS
before I close this @MarkBoots, in regards to your comment. Could this be done with css?
are monitor-body and monitor-leg childs of monitor?
because in that case
if you use a data attribute instead of class, there is no need to remove previous values, it will just override
@MarkBoots monitor-body, monitor-leg are not childs of
monitor
I can try still, incase it might work even if they are children of monitor
. Awesome to know that data attributes don't need to be removedyou can make it a group <g> and add the class to that
ok, gonna look into that
hmm, reading up on
<g>
on MDN, the example they show is targeting the same path of the SVG, so in my case, I'm not sure if I can target different paths by targeting the <g>
do you think it's possible to achieve that with the
<g>
?
it's not always the path with the class of "monitor" that's changing color, on python
and svelte
svg's the body/leg also change
https://jenkov.com/tutorials/svg/g-element.html
don't think it would work in my case
good to know about it though, thank you for the help!