stef
stef

Reputation: 27749

jQuery inserting ajax content

This function inserts a file upload field in a specific div

$(".add_file_field").click(function() {
    var qid = $(this).attr("rel");
    $(this).parent().before("<div id='p_holder'></div>");
    $("#p_holder").load("<?=site_url('survey/add_upload_field')?>", {
        'qid': qid 
    });
    return false;
});

It is triggered by clicking on the link in this snippet

<span class="add_field_wrap">
    <a rel="31" class="add_file_field" href="http://domain.com/kms/#">
        Add another file
    </a>
</span>

How can I get it to insert the content just before the span.add_field.wrap, ie. without specifying a specific container that will receive the content? The span and all other markup must stay in place; the point is that the user can add as many file upload fields as he wants.

Thanks

Upvotes: 0

Views: 78

Answers (4)

ShankarSangoli
ShankarSangoli

Reputation: 69905

Try this

$(".add_file_field").click(function(){
    var qid = $(this).attr("rel");
    if(!$("#p_holder").length){
      $(this).parent().before("<div id='p_holder'></div>");
    }
    $.get("<?=site_url('survey/add_upload_field')?>", {'qid': qid },
     function(response){ 
       $("#p_holder").append(response);
    });
    return false;
});

Upvotes: 0

Rafay
Rafay

Reputation: 31033

$(".add_file_field").click(function(){
    var qid = $(this).attr("rel");
    $(this).parent().before("<div id='p_holder'></div>");
    $("#p_holder").load("<?=site_url('survey/add_upload_field')?>", {'qid': qid },
      function(data){
         $("span.add_field_wrap").insertBefore(data);
    })
   return false;
});

Upvotes: 0

lbrandao
lbrandao

Reputation: 4373

$(".add_file_field").click(function(){
    var qid = $(this).attr("rel");
    var parent = $(this).parent();

    $.get("<?=site_url('survey/add_upload_field')?>", {'qid': qid }, function(data) {
        $(parent).before(data);  
    });

    return false;
});

Upvotes: 2

Tomas
Tomas

Reputation: 59475

Is this what you want?

$('.add_field_wrap').before(required_html_content);

Upvotes: 1

Related Questions