views:

34

answers:

1

I'm trying to do an autocompleter in mootools 1.11. Everything works fine but i just cant check if

this.selectel.getNext()

is null or whatever. Firebug outputs [li] for every element and [null] for the non existing element i output via getNext();

I saw that some people are just doing:

if(this.selectel.getNext()) {...

but thats not working here because i always get the null object. Something terribly stupid must be going on here...

Here comes some code around the problem:

this.selectel = $$('ul.results').getFirst();

...

onCommand: function(e, mouse) {
    if (e.key && !e.shift) {

        switch (e.key) {
            case 'up':                  
                this.selectel.getPrevious().addClass('active');
                if(this.selectel) this.selectel.removeClass('active');
                this.selectel = this.selectel.getPrevious();
                e.stop(); 
            return;

            case 'down':
                var test = this.selectel.getNext();
                console.log(typeof(test));

                if(this.selectel.getNext() != null) {  // not working

                    this.selectel.getNext().addClass('active');
                    if(this.selectel) this.selectel.removeClass('active');
                    this.selectel = this.selectel.getNext();
                }
                e.stop(); 
            return;

        }
    }
A: 

your are calling getNext() twice.

replace these lines:

if(this.selectel.getNext() != null) {  // not working
   this.selectel.getNext().addClass('active');
   if(this.selectel) this.selectel.removeClass('active');
   this.selectel = this.selectel.getNext();
}

with:

if(el = this.selectel.getNext() != null) {  // not working
   el.addClass('active');
   if(this.selectel) this.selectel.removeClass('active');
   this.selectel = el;
}
rahim asgari
Thats not working. i tried this without success:var test = this.selectel.getNext(); if(test != null) { test.addClass('active'); if(this.selectel) this.selectel.removeClass('active'); this.selectel = this.selectel.getNext();}
Mike
all he suggests is caching the selector into el so you don't call it twice. needs var el = though else it becomes window.el which is not desirable.
Dimitar Christoff
I allready stored it into test (see above). But if(test != null) { ... is not getting null but [null] which is not false.
Mike