marcusBMG
marcusBMG

Reputation: 15

jQuery load page into div function

I am using this function to load a page into a div

$(function() {
    $('a[href=myurl]').click(function() {
        $('#loadHere').load('myurl');
        return false;
    });
});

This is working fine I am testing this code for more than one link, which finds the class attribute InPage:

$('a.InPage').click(function() {
    $('#loadHere').load( $(this).attr('href') );
    return false;
});

In my test page: http://www.tremyfoel.co.uk/jqueryTest2.htm

But for some reason not apparent to me in spite of persistent efforts/tinkering doesn't work. Logic & syntax seems fine, jsFiddle doesn't flag any error, I've tried different jQuery versions (for some reason 1.4.0 is whats required). Any advice appreciated.

Upvotes: 0

Views: 2509

Answers (2)

j08691
j08691

Reputation: 208032

Change

<script type="text/javascript">
$('a.InPage').click(function() {
    $('#beachInfo').load($(this).attr('href'));
    return false;
});
</script>

to

<script type="text/javascript">
$(function() {
    $('a.InPage').click(function() {
        $('#beachInfo').load($(this).attr('href'));
        return false;
    });
});
</script>

You need to wrap your other script in the same $(function() { document ready code that the working code exists in. Otherwise the browser attempts to run it before the element has been loaded.

Upvotes: 2

SLaks
SLaks

Reputation: 888233

Your element doesn't exist when you run the second script.

You need to run it after the page finishes loading.

Upvotes: 1

Related Questions