Michael
Michael

Reputation: 13

jQuery load with variable instead of element id

i've got a slight jQuery problem. What i want is to load a particular area of an external html doc into a div, instead it loads the whole document, not the specified area.

This is the trigger:

$('a').click(function(){ // my link to trigger the load
    var pid = $(this).attr('href'); //the pid is the links href
    getproject(pid); //trigger the load function, pass variable
});

This is the triggered function:

function getproject(pid) {
    $('#container').load('data.html', pid);
}

So when i click my link, it should load the element with the id (#) specified by the link into my container, but it loads the whole data... i cant seem to find a solution to this.

The Link looks like this (cant use exact markup here): a href="#elementtoload"

The data document looks like this: div id="elementtoload" div id="..."

and loads of more elements with content, which should be loaded by id from the links href.

Upvotes: 1

Views: 3089

Answers (5)

user729073
user729073

Reputation: 21

I was trying something similar and after some hours (...) michael came finally with something that works :) this did the trick for me, the # should be in the url string, like this:

$('#div1').load('data.html #' + x);

Upvotes: 0

Felix Kling
Felix Kling

Reputation: 816790

String concatenation:

.load('data.html ' + pid);

Regarding your update:

<div id="#elementtoload"> should be <div id="elementtoload">

Upvotes: 0

jbabey
jbabey

Reputation: 46657

Looking at the documentation for $ load, you should be able to do this:

function getproject(pid) {
   $('#container').load('data.html #' + pid);
}

Upvotes: 1

Perception
Perception

Reputation: 80623

Not sure what your actual intent is but it seems to me that you are going to alot of trouble to get the pid but then not using it in your load routine. I'm guessing the code should look more like this:

function getproject(pid) {
    $(pid).hide().load('data.html');
}

Upvotes: 0

Jeff Parker
Jeff Parker

Reputation: 7517

http://api.jquery.com/load/

Second section, Loading Page Fragments. For us to say exactly how this is relevant, you'd need to provide an example of the triggering link, and ideally the document you're loading.

Upvotes: 1

Related Questions