Search code examples
javascriptprototypejs

Prototype.js: How to OVERWRITE a div, not updating it's content via ajax?


When I do

new Ajax.Updater('foo', 'getALifeGuys.php', ...

this will update the content of

<div id="foo">.

But how do i OVERWRITE this div with the result of the ajax call ? The main idea behind this is that we don't know the parent of the foo-div and the result of the ajax call also contains the <div id="foo">.


Solution

  • You can use the insertion option, which takes a function as well:

    new Ajax.Updater('foo', 'getALifeGuys.php', {
        insertion: function(el, response) {
            $(el).replace(response);
        }
    });
    

    If your div#foo is the only child of its parent element, you can as well just pass that parent:

    new Ajax.Updater($('foo').parentNode, 'getALifeGuys.php', …);