Mootools equivalent to jQuery toggle ()?

I am trying to use mootools for the quick task of switching text to click. But I can not find the equivalent Mootools function jQuery toggle ().

I am trying to do the following:

$('a#id').toggle(
 function() {
  $(this).set('html', 'new text');
 },
 function() {
  $(this).set('html', 'old text');
 }
);

How do I change the above code for mootools?

Thank!

+3
source share
2 answers

As far as I know, "toggle" on mootools does not exist.

You could "implement" this, for example, as follows: http://www.jsfiddle.net/steweb/rdvx8/

Is this what you would like to receive?

Edit (for mootools: D developers), this may be a way to create a common “switch”:

Element.implement({
    toggle: function(fn1,fn2){
        this.store('toggled',false);
        return this.addEvent('click',function(event){
            event.stop();
             if(this.retrieve('toggled')){
                 fn1.call(this);
             }else{
                 fn2.call(this);
             }
            this.store('toggled',!(this.retrieve('toggled')));
        });
    }
});

//using it in a 'jQuery' mode

$('mya').toggle(
   function() {
      this.set('html', 'new text');
   },
   function() {
      this.set('html', 'old text');
   }
);

here: http://www.jsfiddle.net/steweb/qBZ47/

2 http://jsfiddle.net/dimitar/UZRx5/ ( @ )

+6

, , mootools 1.3 , Element Pseudos, , , . ,

http://www.jsfiddle.net/dimitar/VR9k8/4/

(function() {
    var toggled = 0;
    Event.definePseudo('toggle', function(split, funcsArray, args){
         if (funcsArray.length && funcsArray[toggled])
             funcsArray[toggled].apply(this, args); // args[0] is the Event instance

         toggled++;
         if (toggled >= funcsArray.length)
             toggled = 0;
     });
})();

document.id("foo").addEvent("click:toggle", [function(e) {
    e.stop();
    alert("function 1");
}, function(e) {
    e.stop();
    alert("function 2");
}, function(e) {
    e.stop();
    // event object (args[0])
    console.dir(e);
    alert("function 3");
}]);

... , , , .

docs: http://mootools.net/docs/more/Element/Element.Event.Pseudos

+5

Source: https://habr.com/ru/post/1786690/


All Articles