views:

1174

answers:

4

I'm trying to load some content into a table using insertAfter(), but the content I'm trying to load is html (output from an asp.net page) that I'm getting using AJAX.Load().

From my understanding, insertAfter() works like this: $("htmlcodehere").insertAfter("selector"). AJAX.Load() works like this: $("selector").load("Html/file"). How would I merge these two so that it loads the html (perhaps without adding it to any existing element?) then inserts that data using insertAfter()?

Bara

+1  A: 

You can use the $.get or $.post to get the html and add a function on success that uses the insertAfter function to inject the code into the table

mck89
I tried something like this, but it's not inserting the html2 file into the right place. In other words, how do I get it to insert in between the spans?$("<tr class='addedrow'><td colspan='4' style='margin: 0; padding: 0;'><div><span></span></div></td></tr>").hide().insertAfter(row).load("/test2.html");Bara
Bara
If you give the span an id, then you could use $("#spanId").html("htmlcode")
Craig Martek
I don't know if i've understood what you want to do but if you want to add the html content beetween the spans you can do: $("<tr class='addedrow'><td colspan='4' style='margin: 0; padding: 0;'><div><span>"+data+"</span></div></td></tr>") where 'data' is the parameter, passed to the success function, that contains the ajax response
mck89
I'm not sure I understand, where would the htmlcode come from? When is the ajax load being done?
Bara
@mck89: you mean after I call the load() I would go into the callback function, then use the response to insert it into the html? Wouldn't that require me to insert the html being loaded somewhere first? eg: $("some selector").load("htmlfile", function() { do insert after here }); .... that would mean I'd need "some selector" first?
Bara
The HTML code come from the file that you request with ajax. Using the $.get or $.post functions you can specify a function that will be executed when the request is complete, this function recive a parameter (the first) that is the html file source code, so inside this function you insert the first parameter (that contains html code) into the table
mck89
That did it! Thanks a lot!
Bara
A: 

You could use JQuery's $.get like this:

$.get("html/file", {}, function(obj) {
    htmlData = obj.data;
    $(htmlData).insertAfter("selector");
}, "html");
Craig Martek
+2  A: 

I try to explain me better with another answer:

$(document).ready(function(){ //If yuo want to load it immediatly. Anyway you execute this code after an event
    $.get("file.html", //Make an ajax request to this file
          function(data){ //data is the file.html content
             $("table").append(data); //Or other operations like this to inject the html content into the table
          })
})
mck89
A: 

Thanks to some help from mck89, this is what I ended up using:

           $.ajax({
              url: "/test2.html",
              cache: false, 
              success: function(data, data2) {
                 if (data2 == "success")
                 {
                    $(data).hide().insertAfter(row);
                 }
              }
           });

Thanks a lot for the help!

Bara

Bara