Topic on Project:Support desk

Trying to create a custom button to old edit toolbar with onClick function

2
Intentandocrearunboton (talkcontribs)

How can I create a new button in the old toolbar with a custom onClick js function?

I'm trying to create it with the addbutton() function but it does not work at all, and I do not know how should I pass my custom function to the addbutton.

p.e:

if ( mw.toolbar ) {
    
          function test(){

                    alert("Hi"); 
                    
          }

          mw.toolbar.addButton({speedTip:"Hi", onClick: function(){prueba()} });
}

The button appears, but when clicking on it, it does not do anything...

any ideas?

Ty!!

Ciencia Al Poder (talkcontribs)

You've defined function test, but at onClick you're calling prueba(). You have to use the same name.

You can even simplify it to:

mw.toolbar.addButton({speedTip:"Hi", onClick: test });

That way, you only need to pass the function name (without the parentheses), because it will work as a function reference itself.

Reply to "Trying to create a custom button to old edit toolbar with onClick function"