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);
}
}