1

Edit: Important: It is extremely unlikely that anybody who has come across this will have the exact same problem as me - check my answer below for my very obscure solution. There were however some useful responses that may apply to other's situations. Good luck if you're having a similar problem.


I've got a really simple jQuery AJAX call :

$("#cart").load("/woops/store/cartsummary"); 

This returns (by means of an ASP.NET MVC Action) some very simple HTML:

<DIV>something</DIV>

On my page I have a destination DIV which is initially populated with some HTML:

<div class="padding15" id="cart">
   <% RenderCart(ViewData.Model.CartItems); %>
</div>

The problem is when the call returns the original #cart div just disappears instead of being replaced. If I look at the DOM in Chrome it is just completely empty. The correct HTML is definitely being returned because I see it in Fiddler.

In addition this code works (but obviously isn't what I want because it is appending) :

$.get("/woops/store/cartsummary", {}, function(data) {
    $("#cart").append(data); 
});

I think I'm just doing something stupid - but I'm switching from Microsoft AJAX to jQuery and I think theres just some small fundamental thing I'm misunderstanding. Where's my DIV gone?

(BTW: 'woops' is a virtual directory to make sure my site has no root relative links during development)

Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
  • Try doing: alert(document.getElementById("cart").innerHTML); in your code once before and once after the ajax call to make sure the ID is correct, and see what the output of the innerHTML is after the ajax call – Ali Feb 11 '09 at 09:07
  • @click-upvote - good thinking. it does give me an HTML string as expected (currently "

    Test

    ")
    – Simon_Weaver Feb 11 '09 at 09:08
  • BTW: $("#cart").load("/woops/store/cartsummary"); is the code taken straight from jQuery in action book (p226) and the implication is it does what I want it to do – Simon_Weaver Feb 11 '09 at 09:09
  • what does it give after the .load request? – Ali Feb 11 '09 at 09:10
  • i just completely stripped down the page to the absolute minimum. it now works with .load(...). just trying to figure out what was breaking it – Simon_Weaver Feb 11 '09 at 09:18
  • one guess, did your page have a tag? in the documentation for the load method it says jquery uses the body tag to find the selector id it is looking for – Ali Feb 11 '09 at 09:21

5 Answers5

1
append

does not replace your div. It pastes the data at the back of your div.

You could try

replaceWith( content ) 

instead.

replaceWith in jQuery documentation

Natrium
  • 30,772
  • 17
  • 59
  • 73
1

Wow! Well that was crazy.

Turns out the problem is because of this code that I have in an included JS file:

 jQuery.fn._init = jQuery.fn.init
 jQuery.fn.init = function(selector, context) {
     if (typeof selector === 'string') {
         return jQuery.fn._init(selector, context).data('selector', selector);
     }
     return jQuery.fn._init(selector, context);
 };

I am using jQuery 1.2.6.

The code above came from this post How do I get a jQuery selector's expression as text?. i think the author since rewrote it, but its breaking this for some strange reason.

The DIV just disappears - but i got thrown because replaceWith() DID work and so did append(). I guess some of the other methods mus internally being tripped up by this but those two werent.

Thanks for everyones quick suggestions. I'm sure they'll help out others some point.

Community
  • 1
  • 1
Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
0

try

$.get("/woops/store/cartsummary", {}, function(data) {
    $("#cart").html(data); 
});

(jQuery docs)

yoavf
  • 20,945
  • 9
  • 37
  • 38
  • i forgot to mention i tried that too. same thing. i think something is just messed up elsewhere in my page - not jQuery though. this is the only jQuery on the page – Simon_Weaver Feb 11 '09 at 09:02
  • Maybe you have another element with that ID? or the code you're loading via ajax has that id in it? – yoavf Feb 11 '09 at 09:05
0

Perhaps try this?

$.get("/woops/store/cartsummary", {}, function(data) {
    $("#cart").html(data); 
});
Ali
  • 261,656
  • 265
  • 575
  • 769
0

Check the ID of your div, make sure the correct ID is being passed on to jquery.

Ali
  • 261,656
  • 265
  • 575
  • 769
  • 1
    @clickupvote - thanks for all your quick tips. ultimate answer was very obscure - see my answer. i had just deadended myself and thought my jQuery was bad which it wasnt – Simon_Weaver Feb 11 '09 at 09:42