Skip to content
Advertisement

Modal doesn’t open on all buttons

As my title already says, I have an issue with a modal not opening on all buttons.

Here is the situation:

I have a page that displays all applications a user has sent for different jobs. So it may be just one, or up to whatever. It looks like this:

image

Now if the user wants to cancel the application he can press the button “Bewerbung zurückziehen”, then the modal opens to give a heads up that all data will be lost and if he is sure, in the modal he can confirm it or go back. Everything works fine for the first post on the site, but for all other posts just nothing happens, so the modal doesn’t open.

Here is my code:

  1. The blade file that displays all posts:

JavaScript

Here is the code for the modal:

JavaScript

Honestly, I have zero clue why it works for the first but not for the others, they have the same buttons, with the same name, id & everything. Maybe one of you had a similar issue. I wish you all happy holidays!

Edit: New code to open the modal:

JavaScript

When I use querySelectorALl then the first one doesn’t work anymore as well

Advertisement

Answer

The id property is expected to be unique within a page, so when using getElementById once an element with that id has been located it doesn’t continue searching for others.

As you have multiple button elements that you want to target, you will need to us something like querySelectorAll or getElementsByClassName to target every element that should trigger the modal to open.

An example to get you on the right direction.

JavaScript
JavaScript

Example JSFiddle

Advertisement