Question

So I have a function that uses jQuery's $.post mechanism to fill in a div with contents from another page. I want to fire this method in the very beginning to show the content in the div right away. To do this I call the method in the document.ready script.

This is how the code looks:

    function OnloadFunction()
{
    alert("HELP!");
    var url = "<?php echo $this->url('public', array('id'=>'ajax')); ?>";
     $.post(url, {contentVar:cv}, function(data){
          $("#shoppingCart").load(data).show();
     });
}

//when document loads, do the following
$(document).ready(function(){
     OnloadFunction();
})

When I load the page, the alert saying "HELP!" shows up, but the $.post function is not making any difference and the div is not filled with the contents from the url.

P.S This shouldn't make a difference but I am using ZF2 (hence the url('public', array('id'=>'ajax')); ?>)

Any help would be appreciated :)

Thanks!

Was it helpful?

Solution

You need to use .html() instead of .load(), assuming data is html content

 $("#shoppingCart").html(data)

OTHER TIPS

The load function takes a URL as an argument, makes an XHR request for it, and then populates the element with the response.

Presumably data is not a URL.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top