Can someone help me create a function that automatically applies to all divs with a specific class on my document? I want the function to check for the presence of another class within those divs and display an image accordingly.
document.getElementsByClassName(".try").forEach({
if(element.classList.contains(".lol"))
{
<img src=""/>;
}
else {<img src=""/>}
});
I know this code is incomplete, but what am I missing? I would greatly appreciate it if someone could provide me with instructions or documentation on how to achieve this.
<div class="try lol"></div>
<div class="try "> </div>
<div class="try lol"></div>
I need the function to iterate through every div with the class "try" and check if it also has the class "lol". If "lol" is present, I want the function to insert an image into the div. I plan to use a loop because there are many divs with the "try" class, each potentially requiring a different image based on the next div's class (e.g. showing image 1 for "lol", image 2 for "lol2").