cmplieger
cmplieger

Reputation: 7371

jquery .load() does not get variable

i have this code

$(document).ready(function () {
    $('a').click(function(e) {
        e.preventDefault();
        url = $(this).attr('href');
        $("body").load(href);
    });
});

But it does not work. Nothing loads. The only thing that works is e.preventdefault()

Am I doing something wrong?

Upvotes: 2

Views: 275

Answers (3)

Chandu
Chandu

Reputation: 82943

You are missing the $ before the (this).attr('href'); and also use the value url for your load function call.

$(document).ready(function () {
    $('a').click(function(e) {
            e.preventDefault();
            url = $(this).attr('href');
            $("body").load(url);
    });
});

Upvotes: 0

Billy Moon
Billy Moon

Reputation: 58619

You have assigned href attr to variable called url, then tried to use href as a variable.

Not good:

url = $(this).attr('href');    
$("body").load(href);

Good:

url = $(this).attr('href');    
$("body").load(url);

Upvotes: 0

David Tang
David Tang

Reputation: 93704

$("body").load(href);

Should be:

$("body").load(url);

Or you could do away with the variable completely:

$("body").load(this.href);

Upvotes: 6

Related Questions