Prototype - insert() Method



This method inserts content before, after, at the top of, or at the bottom of element, as specified by the position property of the second argument. If the second argument is the content itself, insert will append it to element.

Insert accepts the following kind of content −

  • text
  • HTML
  • DOM element
  • Any kind of object with a toHTML or toElement method.

NOTE − Note that if the inserted HTML contains any <script> tag, these will be automatically evaluated after the insertion.

Syntax

element.insert({ position: content });

OR

element.insert(content)

Return Value

Returns HTML element after inserted content.

Example

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         function showResult() {
            var str = $('apple').insert(  "<li>mangoes</li>" );
            alert(str.innerHTML );
         }
      </script>
   </head>

   <body>
      <p>Click the button to see the result.</p>
      
      <ul>
         <li id = "apple">apple</li>
         <li>orange</li>
      </ul>
      <br />
      
      <input type = "button" value = "Click" onclick = "showResult();"/>
   </body>
</html>

Output

prototype_element_object.htm
Advertisements