Using getNext() with Mootools
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 doi开发者_运维技巧ng:
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;
}
}
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;
}
精彩评论