Best practice to include small portion of javascript

I have a doubt in a form I need that a checkbox field run a small javascript function, what is the best approach to add this javascript code in my file?

At the moment I have 2 options:

  1. create the function directly on the code, this is more readble than option 2


    function myfunction{

        //do something



  1. use the php function (registerJs) to add the code,


$this->registerJs("function myfunction{

        //do something



What is the best approach? If there are other better option please tell me


The best approch for a little script is the registerJs (good practice).

Why ? Because of positionning first witch is by default : POS_READY (enclosed within jQuery(document).ready())

It’s important because sometimes you use your code in view inside a view inside a view …

Other solution exist :

  • registerJsFile if you want to create library or use external script.

  • playing with assets

But for little functions => registerJs is suffisant