Click here to Skip to main content
16,011,374 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I need a modals[index] so I can read modal 1 from start?

HTML
<h2>1st Modal</h2>


Open Modal


<div id="myModal1" class="modal">

  
  <div class="modal-content">
    <div class="modal-header">
      ×
      <h2>Modal Header</h2>
    </div>
    <div class="modal-body">
      <p>Some text in the Modal Body</p>
      <p>Some other text...</p>
    </div>
    <div class="modal-footer">
      <h3>Modal Footer</h3>
    </div>
  </div>

</div>

<h2>2nd Modal</h2>


Open Modal


<div id="myModal2" class="modal">

  
  <div class="modal-content">
    <div class="modal-header">
      ×
      <h2>Modal Header</h2>
    </div>
    <div class="modal-body">
      <p>Some text in the Modal Body</p>
      <p>Some other text...</p>
    </div>
    <div class="modal-footer">
      <h3>Modal Footer</h3>
    </div>
  </div>

</div>



[edit]
Added by OP as a solution:
Okay.

Creating Multiple Modals on a Single Page

https://www.codeproject.com/Questions/5384491/From-multiple-modals-from-open-start-or-load-page

Yes I understood it.

Now I wanted a open modal from loading start page. I know it click by index 0 - 1 or 1 - 2

My question goes, how can you open modal 1 in a body using javascript ,

and then open it!

Thanks
[edit]

What I have tried:

JavaScript
<body onload="openstartpage();">

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

// All page modals
var modals = document.querySelectorAll('.modal');

// Get the <span> element that closes the modal
var spans = document.getElementsByClassName("close");

// When the user clicks the button, open the modal
for (var i = 0; i < btn.length; i++) {
 btn[i].onclick = function(e) {
    e.preventDefault();
    modal = document.querySelector(e.target.getAttribute("href"));
    modal.style.display = "block";
 }
}

// When the user clicks on <span> (x), close the modal
for (var i = 0; i < spans.length; i++) {
 spans[i].onclick = function() {
    for (var index in modals) {
      if (typeof modals[index].style !== 'undefined') modals[index].style.display = "none";    
    }
 }
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target.classList.contains('modal')) {
     for (var index in modals) {
      if (typeof modals[index].style !== 'undefined') modals[index].style.display = "none";    
     }
    }
}

I need a modals[index] so I can read modal 1 from start?
Posted
Updated 28-Jun-24 18:27pm
v3
Comments
Richard Deeming 28-Jun-24 11:25am    
"I need" and some unexplained code does not make this a good question.

Start by explaining precisely what you are trying to achieve, what you have tried, and where you are stuck. Include the full details of any errors.

I'm sure "I need a modals[index] so I can read modal 1 from start" means something to you; but the rest of us don't have the faintest clue.
Mark Rene Jensen 28-Jun-24 11:49am    
Ok new question is written

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900