Prototype - replace() Method



This method replaces element by the content of the html argument and returns the removed element.

Syntax

element.replace(html);

Here html can be either plain text, an HTML snippet or any JavaScript object, which has a toString() method.

If it contains any <script> tags, these will be evaluated after the element has been replaced.

NOTE − If no argument is provided, Element.replace will simply clear element of its content.

Return Value

Returns the removed HTML element.

Example

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         function showResult() {
            $('first').replace('<ul id = "favorite">' +
               '<li>kiwi</li>' +
               '<li>banana</li>' +
               '<li>apple</li>' +
            '</ul>');
         }
      </script>
   </head>

   <body">
      <p id = "test">Click the button to see the result.</p>
      
      <div id = "food">
         <div id = "fruits">
            <p id = "first">Kiwi, banana <em>and</em> apple.</p>
         </div>
      </div>
      
      <input type = "button" value = "Click" onclick = "showResult();"/>
   </body>
</html>

Output

prototype_element_object.htm
Advertisements