tags:

views:

189

answers:

3

I need to load some ajax content after the fieldset with the class address. Using the code below it will replace the content of the .address with the ajax content.

    $('.accordion .head').click(function() {
    $(this).next().find('.address').after().load('http://test.dev/search/view/IdDaytrip/' + $(this).attr('id') + '.html');
    $(this).next().toggle('slow');
    return false;
}).next().hide();

<div style="display: none;">
<fieldset class="address">
<!-- AJAx CONTENT GOES HERE -->
    <ol>
        <li>Stadhouderskade 78</li>
        <li> Amsterdam-Zuid</li>
        <li>020-5239666</li>
    </ol>
</fieldset>
<!-- BUT THE AJAx CONTENT NEEDS TO GO HERE -->
<span class="tags">Tags: museum</span>
</div>
+2  A: 

I usually do that this way:

var clicked = $(this);
$.get('http://test.dev/search/view/IdDaytrip/' + $(this).attr('id') + '.html',
function(data){
    alert("Data Loaded: " + data);
    $(clicked).next().find('.address').after(data);
});
rsilva
Your first example will not work, because load is an asynchronous function so it cannot return the results to a variable..
Gaby
thanks Gaby, first example removed
rsilva
A: 

This should work ..

$('.accordion .head').click(function() {
    $next = $(this).next();
    $.get('http://test.dev/search/view/IdDaytrip/' + $(this).attr('id') + '.html',
          function(data){
                         $next.find('address').after( data );
                        }
         ).end().toggle('slow');
    return false;
}).next().hide()
Gaby
A: 

Actually after requires the object to append after given element. when you call

$(this).nex().find('addresss').after()

you are returned the reference to:

'address' + [content appended by `after`]

as we have not given anything to after function to append to address so we are still calling load on $('.address').

use this script instead:

    $('.accordion .head').click(function() {
    $(this).next().find('.address').after($("div/>")
     .load('http://test.dev/search/view/IdDaytrip/' + 
         $(this).attr('id') + '.html'));
    $(this).next().toggle('slow');
    return false;
}).next().hide();

this creates new div to wrap the ajax content and show it in right way.

TheVillageIdiot