1

Problem: On the html page, it is able to run the JS file once (allow a the modal to pop out and display the contents) however the button "Full Listing" is only able to work on the first option and each page (different search which filters the rows returned). Every other click of full listing button other than the first does not do anything.

Button and JS which is a part of the same php file

 <script> var modal = document.getElementById("Modal");

// Get the button that opens the modal
var btn = document.getElementById("Button");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
2
  • 1
    Please post the part containing the element with ID Button Commented Oct 15, 2021 at 1:56
  • @Ken Is this what you mean? <button id="Button">Full Listing</button>. That's what I get when copying the element on my chrome console. Sorry for my ignorance, I'm new to web development so all this terminology is new to me. Edit: i.imgur.com/HJABAgP.png Commented Oct 15, 2021 at 1:59

1 Answer 1

1

An ID is expected to be unique. The DOM will only qualify the first ID = 'Button'. You will need to use class="Button"

then use:

var btn = document.getElementsByClassName("Button")

This also applies to ID="Modal". It will have to be changed to a class if every row has it's own modal.

The tricky part will be to associate which button shows/hides which modal. There's many different ways this can be done. I would show one way here, but you're not using jQuery so I'm not familiar with doing it with document objects.

Sign up to request clarification or add additional context in comments.

2 Comments

That worked! Thank you so much. They're all viewing without clicking now though? Not quite sure how to fix this.
Yes, that's what I was referring to in my answer when I mentioned "The tricky part..." Basically your btn.onclick function is going to show all elements with class="modal". You need to tell it to show only one modal. Usually the modal will be a hidden div next to the button (like a neighbor), and then whichever button is clicked, you show it's neighbor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.