Hello everyone, I’m currently coding the javascript for an e-commerce website and using javascript to add the html for each product using a loop. This means that for each product I have a delete button like this :
<p id="delete" class="${productTitle}" onclick="removeItems()">Delete</p>
I would like to recover the class of this to then use it to remove the product it is refering from the local storage but because I have a loop if I have a function such as
function removeItems() {
console.log(${productTitle});
}
I am only getting the last assigned value of ${productTitle}
How can I make sure that I get the correct class that the button is using please?
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
This is slightly off topic, but I couldn’t help myself
That is not a button. For accessibility, you should be using an actual <button> element. Adding a click handler to a p element does not make it accessible.
It would be nice with a working example (with the products).
You can get the event.target inside the handler to get the click target (the button). From there you can do DOM traversal to get to any element from the click target (parent, siblings). When you have the element you want you can look at the attributes or content of the element.
Haha it did seem odd but for the javascript side of things it shouldn’t change much I guess! I just can’t seem to figure out how to get it to work, here is a link to the github hosted code as it is currently if this helps: Kanap
as you’ll see, the console(log) is showing the same productTitle no matter which delete button is clicked on!
Here it is so far Kanap
As you’ll see the console gives out the same value of productTitle no matter which delete button is clicked…
I agree with you, that seems like a good way to do it but I just can’t seem to get it working
If you are going to assign the event handler using the onclick attribute then you can’t just call the function like that because you won’t have access to any of the event details, such as the button that was clicked. One way to get access to those details is to use an IIFE instead:
And now you have access to the actual button you clicked on through event.target and so you can find the class for the button and anything else you need to know about the button.
One other thing, if you are planning on having multiple delete buttons on the page at one time then you can’t give them all the same id of delete. ID’s are supposed to be unique on the page.
Thank you for taking the time to reply, with the code that you provided what can I add in the function in order to get the class? I will remove the on the article sections, it was just to test something I forgot to remove!
Ahh, of course you’re right. I guess I was thinking it couldn’t be that easy I never use the onclick attribute and I guess I was thinking it had some limitations.
You do not actually need to pass or accept the event. You have access to the event object inside the handler, it is called event inside the handler. But I don’t think being explicit about it is bad.
I update a random Codepen I had with some quick button logic.