Prototype element insert example

1

Posted by nick | Posted in JavaScript | Posted on Aug 21 2009

Tags: ,

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} );
  • Share/Bookmark

Comments (1)

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


Write a comment