views:

56

answers:

2

Lets say I have this:

<object class="MyClass" type="text/html" data="/Whatever/1?renderpartial=1"></object>
<object class="MyClass" type="text/html" data="/Whatever/2?renderpartial=1"></object>

And, I want to use jQuery to replace the object with the actual HTML of the object.

<script type="text/javascript">
    $(document).ready(function() {
        $(".MyClass").before('<div class="MyClass">#CONTENT#</div>').remove();
    });
</script>

I want to fire off an asynchronous request to go get each of '/Whatever/1' and '/Whatever/2' from the server and put it in place of '#CONTENT#'.

Is this possible?

+1  A: 

you can use the jquery load function:

$(".MyClass").each(function(){
  $(this).replace("<div>").load($(this).attr("data"));
});
Marius
+1: Ok, that looks pretty good, but I want to turn the OBJECT into a DIV and `load` the DIV with the URL of the original object. Can that be done cleanly?
John Gietzen
+2  A: 

To replace the original object as a div:

$(function(){
    $('.MyClass').each(function(){
        var $current = $(this);
        $.post($current.attr('data'), function(data){
            $current.replaceWith('<div>' + data + '</div>');
        })
    });
});
seth
Also +1, but I want to turn the OBJECT into a DIV as I'm doing this...
John Gietzen
This give me 'this.attr' is not a function...
John Gietzen
Try using $(this) instead.
seth
Well, that mostly works, but there seems to be a bug in my version of jquery that is preventing the replaceWith from executing properly... The HTML is being fetched just fine tho... The error is `Error: (this[0].ownerDocument || this[0]).createDocumentFragment is not a function`
John Gietzen
Ok, that was a scope error on my part. I updated my example, so try it once more using the updated code.
seth
That works perfectly, thank you.
John Gietzen