2

I'm trying to replace "Hello world!" with "Hello to you too" using a ajax replaceWith. However I'm getting this error:

Cannot read property 'replaceWith' of null

Where am I taking the wrong turn?

NOTE: the ajax call works with alert(response);

index.phtml:

<div id="helloworld">Hello world!</div>

<script type="text/javascript">
    jQuery.ajax({
        url: "<?php echo $this->getUrl('topperproductqa/index/ajax') ?>",
        success: function(response){
            $('#helloworld').replaceWith(response);
            //alert(response);
        }
    });
</script>

IndexController.php:

class Topper_ProductQA_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        $this->loadLayout();
        $this->renderLayout();

        return $this;
    }

    public function ajaxAction ()
    {
        $response = "Hello to you too";

        $json =  json_encode($response);
        $this->getResponse()->setHeader('Content-type', 'application/json');
        $this->getResponse()->setBody($json);
    }
}
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339

1 Answers1

1

I figured out Magento is using $ on prototype.js, I fixed it with:

(function($) {

    $.ajax({
        url: "<?php echo $this->getUrl('topperproductqa/index/ajax') ?>",
        dataType: "json",
        success: function(response){
            $("#helloworld").replaceWith(response);
            //alert(response);
        }
    });

})(jQuery);