8

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?

0

5 Answers 5

14

Use jQuery.click and jQuery.load:

$(document).ready(function(){
    $('.classloader.').click(function(){
        $('#contenthere').load('/includes/about-info.html');
    });
})
Sign up to request clarification or add additional context in comments.

Comments

4

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>

Comments

1

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

Comments

0

Try the following:

var myurl = 'myfileonthesamedomain.html';

$('button').click(function(){
    $('div.loadme').load(myurl);
});

Comments

0

Use this simple and sobher it also do run time binding as well:

$(document).ready(function(){
    $(document).on('click','.classloader',(function(){
        $('#contenthere').load('/includes/about-info.html');
    });
})

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.