cynclabs
cynclabs

Reputation: 23

jquery/ajax loading href onClick for multiple links

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') ; 
} );

Upvotes: 1

Views: 545

Answers (2)

Hamid Javadi
Hamid Javadi

Reputation: 211

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>

Upvotes: 0

CumminUp07
CumminUp07

Reputation: 1978

You can get the href by using this

$('.classofButton').click ( function () {
     $('#content').load ($(this).attr('href')) ; 
} );

Upvotes: 1

Related Questions