denisjacquemin
denisjacquemin

Reputation: 7414

Element.insert at bottom-1 with prototype

I want to insert an Element at the position bottom -1 of his parent.

The following code insert at the bottom

el.insert({bottom: content})

thanks

Upvotes: 2

Views: 716

Answers (1)

npup
npup

Reputation: 2541

You can insert it "before the last child element" like this:

el.select('*').last().insert({before:content});

el.select('*') gives the child elements in a nice prototype-extended collection.

last() of course retrieves the last element of that collection. You might want to retrieve it separately and make sure it is not undefined (which is what will be returned if there are no children) before attempting to "insert before it".

Also, IMO it feels better to pass in a non-wildcard selector if possible. For example, when the containing element is an <ul>, pass in 'li'.

Links:
Prototype Element#insert
Prototype Array#last

Upvotes: 2

Related Questions