how to use jquery in yii2


Can some one give me an example on how to use jquery in yii2. I want to have two buttons in one form (a search button and a submit button) and each one of these buttons triggers a different action on click. can you give me a simple example on how to use jquery when clicking on a button to excute a specific controller’s action.

I dont know where to declare the script, is it in the view and how to make the relation with the controller actions.

Thank you

You can define it into asset file which normally reside in web/assets directory. Refer sample code below.

class AppAsset extends AssetBundle


    public $basePath = '@app/web';

    public $css = [




    public $js = [




    public $depends = [






You can write your jquery into custom.js file

you can use jquery in at the end of script _form.php or in create.php



$script = <<< JS

$(".dynamicform_wrapper").on("beforeInsert", function(e, item) {



$(".dynamicform_wrapper").on("afterInsert", function(e, item) {



$(".dynamicform_wrapper").on("beforeDelete", function(e, item) {

    if (! confirm("Are you sure you want to delete this item?")) {

        return false;


    return true;


$(".dynamicform_wrapper").on("afterDelete", function(e) {

    console.log("Deleted item!");


$(".dynamicform_wrapper").on("limitReached", function(e, item) {

    alert("Limit reached");