Element.style.backgroundImage not working on class

(Markus Bjerre) #1

Hello there! I’m curious if there is a way to toggle what background image to show or not.
I get it working with getElementById but can’t make it happen with getElementsByClassName.
I’m curious if it’s even possible to change the image on a class.

It is very important that it is the class and not the div, since the flagicon will be displayed on several scenes and needs to be the same (toggled or not) to the new scene. I don’t want to do this with timelines, button press in element inspector or anything like that. It needs to be made with pure javascript or jquery to make it work with the rest of the code that is not added in this example.

If there is no way of doing it I will make an external css for this and add remove the class, feels like it should be possible without css though.

Anyway, here is the example:
toggleflagnotworking.hype.zip (78.1 KB)

(Mark Hunte) #2

getElementsByClassName. returns an Array. So you would need to iterate over it.

But I would use symbols and timelines for this.

This example give you an idea using symbols, timeline in symbols and javascript. Two buttons. 1 uses JS and a time line

toggleflagnotworking2.hypetemplate.zip (86.5 KB) the other just a time line

(Markus Bjerre) #3

:o so that’s what symbols are good for I guess :slight_smile: Thanks @MarkHunte that worked very well. I modified it a bit to place it within the jquery code (got a lot of keybinds as well so it has to work well with them and it did).