Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an element to 'body' using Prototype?

I have created an element like this:

var myDiv = new Element('div');
myDiv.update('Hello!');

I want to add myDiv to body.

I tried

$('body').insert(myDiv);

But it is not working. I also tried

$('body')[0].insert(myDiv);

thinking that $('body') was returning an array. Even that didn't work.

How can I add myDiv to body?

Thanks.

like image 219
Senthil Avatar asked May 10 '10 08:05

Senthil


2 Answers

How about

$(document.body).insert(myDiv);

?

Differently from jQuery, in Prototype, $('body') fetches the element with the id body.

like image 194
Pekka Avatar answered Sep 28 '22 06:09

Pekka


$ is a shorthand for document.getElementById(), $$ is the more versatile prototype function. To access the (first) body element in your document, use:

$$('body')[0].insert(myDiv);
like image 33
Salman A Avatar answered Sep 28 '22 06:09

Salman A