Search code examples
mootoolsinject

Mootools 1.3 How can I have get an if statement inside an "Inject" function?


I need the following if statement inside the adopt code, but its not valid if it is there. is there some way I can do the same some other way?

var ele = new Element('li').adopt(

    new Element('span.title', {text:row.title}),
    new Element('span.subtitle').adopt(
        // Need this to work, but its invalid where it is atm
        if(row.subtitle = 1)
        {
            new Element('img', {src:'images/subTitle.png'})
        }
    ),
    new Element('span.bold', {text:row.bold}),
);

ele.iject(...

Solution

  • I'm not very familiar with MooTools, but I don't see why this wouldn't work:

    var subtitle = new Element('span.subtitle');
    if (row.subtitle == 1) subtitle.adopt(new Element('img', {src:'images/subTitle.png'}));
    
    var ele = new Element('li').adopt(
        new Element('span.title', {text:row.title}),
        subtitle,
        new Element('span.bold', {text:row.bold}),
    );