Benn
Benn

Reputation: 5023

How to create and inject multiple elements with Mootools?

I have 5 a elements that I need to inject with additional span , instead of creating new Element 5 times how can I do this only once ? I tried

var holders= $$('.holders');
holders.each(function (el){
var addspan = new Element('span', {
    'class': 'over'
});
    el.inject(addspan , 'top');
});

but it does not work any help is appreciated , thank you!

Upvotes: 2

Views: 641

Answers (2)

nwellnhof
nwellnhof

Reputation: 33638

Like akaIDIOT already mentioned, you have to swap addspan and el when using the inject method. To save a line of code, you can chain the inject method with the new element like this:

var holders= $$('.holders');
holders.each(function(el) {
    var addspan = new Element('span', {
        'class': 'over'
    }).inject(el, 'top');
});

Upvotes: 1

akaIDIOT
akaIDIOT

Reputation: 9231

Injection works the other way around; it injects an element into another one. Try reversing addspan and el.

Another option would be to use the adopt function, which might be more intuitively meant to have an element adopt another one.

Upvotes: 4

Related Questions