1

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') ; 
} );
1
  • Try using $(this) to get clicked element and work on top of it achieve what you want. Commented Dec 17, 2019 at 19:35

2 Answers 2

1

You can get the href by using this

$('.classofButton').click ( function () {
     $('#content').load ($(this).attr('href')) ; 
} );
Sign up to request clarification or add additional context in comments.

Comments

0

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>

Comments

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.