Skip to content
Advertisement

loop through wordpress articles and add a css class with js

I hope you have a good day ๐Ÿ™‚

I am working on a plugin currently. I would like to loop through all the articles: on click => open a popp-up, when the pop-up closes => show this content โ€ฆ My code only works for the first article. Sorry if that seems trivial to you, if you have links or tutorials to advise me, I am interested ๐Ÿ™‚

Thank you !

JavaScript

Advertisement

Answer

It seems to be a problem with selecting the elements in the document.

You can use next selector: https://api.jquery.com/next/ instead of selecting all and looping with foreach. With next, you will get the closest element.

Suppose all the posts in your list have a button with the class trigger and when clicked it shows a popup with the class of popup.

JavaScript

This way the click / action (you want to have it when closed) on (this) element will affect nearest element.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement