skip to Main Content

trying to load html page to div using jQuery ajax im pretty sure it’s right but i don’t know why exactly it’s not working.
that’s my code:

 <div class="second">

</div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript">
  $(function(){
    $("button").on("click",function(){
    $('#second').load($(this).data("page"));
  });
  });
  </script>

2

Answers


  1. Change:

    $('#second')
    

    To:

    $('.second')
    

    As second is a class, not an id.

    Login or Signup to reply.
  2. Consider the following code.

    $(function() {
      $("button").click(function() {
        $(".second").load($(this).data("page"));
      });
    });
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    
    <button data-page="page-2.html">Get Page 2</button>
    
    <div class="second"></div>

    This will work properly as it uses a Class selector to target he proper <div> element.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search