Prototype element insert example

1

If you’re like me, you found the documentation on prototype’s insert method lacking much needed documentation http://www.prototypejs.org/api/element/insert . They say the format is like

insert(element, { position: content }) -> HTMLElement

What the heck does that mean? Obviously you can’t just paste it in because it’ll cause a JavaScript error. After some googling and trial and error, I have the code working and will provide it as an example. The code create a div with content and places it at the bottom of an existing page element.

//create new div
var myDiv = new Element( 'div' );
 
//put product name in the new div
myDiv.update('text inside the div');
 
//append div to page element
Element.insert($('pageElement'), {'bottom':myDiv} );
Posted in: JavaScript

This article has 1 comment

  1. Prasun 11/21/2009, 10:47 am:

    Can I insert a div at the bottom of a div element?
    I tried to do
    $(‘mydiv’).insert(new Element(“img”,{id:”myimage”,src:”images/ltr.png”}),{bottom:”myimage”});

    mydiv is my parent div and I want myimage to be at the bottom of mydiv. Is it possible in any way

Leave a Reply

Your email address will not be published. Required fields are marked *

*




You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">