Lets say i have this:
$('a').each(function() {
$(this).click(function(e) {
e.preventDefault();
var href = $(this).attr('href');
$('#somediv').load(href + ' #foo');
});
});
Now how would I make it load the inner contents of #foo and not the actual div #foo
Still not quite sure what I mean?
<div id="foo">
<!-- Load these divs only -->
<div class="children">bar</div>
<div class="children">bar</div>
<div class="children">bar</div>
<div class="children">bar</div>
<div class="children">bar</div>
<!-- // -->
</div>
I want to load the inner contents of a div only. Any help is greatly appreciated!
EDIT: SOLVED
I used the unwrap method:
parent.load(href + ' #' + ident + '', function() {
$('#'+ident+' > div').children().unwrap();
});
You can do this fairly easily if you don’t mind using
$.getand doing the loading parts by hand, something like this:This grabs the full chunk of HTML from
hrefusing$.getand then, in the success callback, we find theid="foo"element inside the whole pile of HTML, extract its content with.html()and then copy thing into#somedivwith the mutator form of.html().If you know that
#foowill only contain<div>children, then you could try this:I’m not certain that this will work and I don’t have a decent test case set up but the
.loaddocumentation indicates that it should work. If#foodoesn’t contain only<div>s then you’ll have to come up with something else to use with the child selector (>). Of course, if#foocontains text that isn’t wrapped in an element, then I think you’re stuck with the$.getapproach above.