Hey, I'm working on an application that renders a collage of images (album
covers). However, whenever I run this code, the update function doesn't send an
alert, so I'm assuming it cannot find any elements with the name I assigned with
the onClick function. Why is that? The render function works fine and assigns
the name properly. The classes are in PHP, but the user input (click events,
etc.) is handled in Javascript.
Here is the render function which is located in the Album class:
Hi Douglas,
did you check the "Console" tab in the developer tools (F12) for error messages?
Usually, you can find what's wrong there.
I wouldn't use the name attribute to select items, this is
usually done by assign a class, e.g. a .selected class. I'd also
use a single JavaScript file to handle all of that. It's always a good idea to
separate JS code from PHP code because it's difficult to find an error
otherwise.
I'm not sure why do you use the <input> element to render
images? I'll use the <img> element instead.
I assigned the .album-image class to the images so I can
manipulate with them in JS easily.
Your JS file can look as follows:
let images = document.getElementsByClassName('album-image'); // selects all the imagesfor (let image of images) { // iterates through all of them
image.onClick = function() { this.classList.add('selected'); }; // adds the onClick event to all of them
}
I'm not sure about your update() PHP function. It would be a big
help if you specified what the application should do.