1

There is search button inside a div and a table inside another div.What I want is, when a user clicks on a button, this Jquery function will show him/her that table content after page reload.I'm using a Jquery function but it's not working on my end.Your help will be much appreciated.Thank you!

HTML:

<div class="col-md-4 search-btn">
   <button id="scroll" type="submit" name="submit" class="btn btn-primary">Search</button>
</div>

This is a table that I want user to scroll to:

<div class="table-responsive">
  <table class="table table-bordered table-striped">
                    <thead style="font-weight:bold; background-color:cornflowerblue;">
                        <tr align="center">
                            <td>No.</td>
                            <td>Title</td>
                            <td>Author</td>
                            <td>Publication</td>
                            <td>Status</td>
                        </tr>
                    </thead>
                    <tbody>
                        <?php

                            foreach ($display as $key) { ?>

                        <tr align="center">
                            <td><?php echo $count++;?></td>
                            <td><?php echo $key['ebook_title'];?></td>
                            <td><?php echo $key['ebook_author'];?></td>
                            <td><?php echo $key['ebook_publication'];?></td>
                            <td>
                                <a href="./admin/ebooks/<?php echo $key['ebook_url'];?>" type="application/octet-stream" class="btn btn-round btn-info" download>Download</a>
                            </td>
                        </tr>
                        <?php } ?>

                    </tbody>
                </table>
            </div>

Jquery:

 <script type="text/javascript">
 $("#scroll").click(function() {
    $('html,body').animate({
        scrollTop: $(".table").offset().top},
        'slow');
});
 </script>

1 Answer 1

1

It works perfectly fine here: https://jsfiddle.net/nfb5zc3d/ Are you sure that the JavaScript comes AFTER the HTML? Otherwise it won't be able to bind the event to the #scroll-button.

If you want to scroll AFTER a page reload, you have to inject the JavaScript into the page using PHP like for example:

    <?php
    if(isset($_POST['submit'])) {
    ?>
    <script>
      $( document ).ready(function() {
         $('html,body').animate({
           scrollTop: $(".table").offset().top},
           'slow');
      });
    </script>
    <?php
    }
    ?>
Sign up to request clarification or add additional context in comments.

4 Comments

I've placed it after body tag @Mads K
Then i assume you issue is that the page reloads, and it won't be able to remember that you clicked the button after the page-reload... therefore you have to use PHP to inject the scrolling-jQuery-part
Could you please provide that JavaScript code? @Mads K
Thank you so much :) @Mads K

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.