skip to Main Content

Building a menu inside of ionic 4 progressive web application and I am using ajax /jquery to load pages into a div. Is there any way to tell the jquery / to load the href src= of the clicked element with the specific class. instead of adding this same code 20 times to load each different page

$('.classofButton').click ( function () {
     $('#content').load ('href of clicked object or link etc') ; 
} );

2

Answers


  1. You can get the href by using this

    $('.classofButton').click ( function () {
         $('#content').load ($(this).attr('href')) ; 
    } );
    
    Login or Signup to reply.
  2. You can do it like this.

    $('.classofButton').click(function(event) {
      event.preventDefault();
      
      var href = $(this).attr('href');
      $('#content').load(href) ; 
    
    });
    #content {
      width: 200px;
      height: 200px;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <a class="classofButton" href="http://google.com">google.com</a>
    <a class="classofButton" href="https://yahoo.com">yahoo.com</a>
    <div id="content"></div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search