views:

122

answers:

3

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.

+1  A: 

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

$$('body')[0]
Salman A
It worked. Thanks. Since the documentation said `$$(cssRule...) -> [HTMLElement...]`, I thought I could only give css class names when using $$ and not tag names! I feel stupid :D
Senthil
`$$('body')[0]` "works", but is inefficient and unnecessary. Instead, use `$(document.body)` as Pekka suggested.
npup
+1  A: 

How about

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

?

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

Pekka
Hi, $(body).insert(myDiv) doesn't work. Did I miss something?
Senthil
@Senthil apologies - it should have been `document.body`. Anyway, `$$` seems to work fine so I assume it's sorted.
Pekka
A: 

$$('body')[0] works fine

denisjacquemin