Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery/AJAX - Load content into a div when button clicked?

Would someone be able to help here?

I would like to fill a div e.g.

<div id="contenthere"></div>

with content from an external file e.g.

/includes/about-info.html

when a button with a certain class is clicked e.g.

<p class="classloader">Click Here</p>

Does anyone have a quick code example they can show me to achieve this?

like image 706
CodeyMonkey Avatar asked Mar 20 '12 13:03

CodeyMonkey


3 Answers

Use jQuery.click and jQuery.load:

$(document).ready(function(){
    $('.classloader.').click(function(){
        $('#contenthere').load('/includes/about-info.html');
    });
})
like image 132
Mariusz Jamro Avatar answered Nov 14 '22 16:11

Mariusz Jamro


Load latest version of jQuery:

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>

jQuery code:

<script language="javascript">
$(function(){
  $(".classloader").click(function(){
    $("#contenthere").load("/includes/about-info.html");
  });
});
</script>

HTML code:

<p class="classloader">Click Here</p>
<div id="contenthere"></div>
like image 37
m1k1o Avatar answered Nov 14 '22 16:11

m1k1o


You could subscribe to the .click() event of the paragraph and then use the .load() function to send an AJAX request to the given url and inject the results into the specified selector:

$(function() {
    $('.classloader').click(function() {
        $('#contenthere').load('/includes/about-info.html');
        return false;
    });
});
like image 1
Darin Dimitrov Avatar answered Nov 14 '22 16:11

Darin Dimitrov