Search code examples
htmlcsshyperlinkanchorpopupwindow

How to add an anchor link on a popup window to jump between sections inside of the same popup window


I'm having a search panel invoked with popup window, which has sections with search results. So I'm trying to add buttons with anchor link for faster navigation on this popup window, because this search can have long results list with scroll.

Here is the code in SearchPopup template

<div><a href="#Unit-link">Unit</a></div>
<h4 id="Unit-link">Unit</h4>

So when I add the anchor like in code example generated link is the page link on which popup was invoked instead of popup itself. So it look like:

  • DefaultPage
    -- SearchPopup
    -- Section "Unit-link" on SearchPopup

I would expect it to generate SearchPopup#Unit-link,
but it generates DefaultPage#Unit-link

Please help! Thank you.


Solution

  • I have found that what you have already will work. The only think you might have to clarify is the word "pop-up". If you mean "modal", then this solution will work. I will make a snippet so you can try it out yourself.

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <title>Bootstrap Example</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
    </head>
    <body>
    
    <div class="container">
      <h2>Modal Example</h2>
      <!-- Trigger the modal with a button -->
      <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
    
      <!-- Modal -->
      <div style="height: 250px;" class="modal fade" id="myModal" role="dialog">
        <div class="modal-dialog">
    
          <!-- Modal content-->
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal">&times;</button>
              <h4 class="modal-title">Modal Header</h4>
            </div>
            <a href="#1">Jump to section 1</a><br />
            <a href="#2">Jump to section 2</a><br />
            <a href="#3">Jump to section 3</a><br />
            <div id="1"class="modal-body">
              <p>1Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
            </div>
            <div id="2" class="modal-body">
              <p>2Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
            </div>
            <div id="3"class="modal-body">
              <p>3Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
          </div>
    
        </div>
      </div>
    
    </div>
    
    </body>
    </html>
    

    Here the internal link is at the top of the modal, the model needs to be shorter than the content inside of it, or else there will never be a scroll bar for the content, which means the link will never jump to that section. Play around with this to get the desired result.

    Cheers!