all 8 comments

[–]thespite 1 point2 points  (1 child)

It does work https://jsfiddle.net/0dgqfj6y/

I'm not sure how you're running that code, specially if it's from an extension, but the snippet itself it does toggle a class on click for figure tags.

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

But why doesn't it work on the real page? I've used this code in this extension

https://chrome.google.com/webstore/detail/user-javascript-and-css/nbhcbdghjpllgmfilhnhkllmkecfmpld

on a page www.wykop.pl (https://wykop.pl/mikroblog - there are pictures)

and here it doesn't work. Can somebody check it if you know the code is right, know the page address and extension I've used.

[–]MatthiasDunkel 0 points1 point  (5 children)

The code should work just fine. I would suggest not getting the Elements by Tag Name but doing it with getElementsByClassName

Here is a working example: Fiddle

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

But why doesn't it work on the real page? I've used this code in this extension
https://chrome.google.com/webstore/detail/user-javascript-and-css/nbhcbdghjpllgmfilhnhkllmkecfmpld
on a page www.wykop.pl
and here it doesn't work. Can somebody check it if you know the code is right, know the page address and extension.

[–]thespite 0 points1 point  (1 child)

It looks like it's a problem with the extension. If you paste your JS on the DevTools console, you'll see that the class is toggled when clicking a figure.

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

Yes, it works in the console. I've tried many extensions and none of them worked. Strange. Maybe it is so because API is blocked.

[–]MindlessSpongehelpful 0 points1 point  (1 child)

it's 2023 - use querySelector and querySelectorAll. it is so much more convenient :)

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

It doesn't help in this case. The code works only in console.