insert javascript in yii2 layout file -
i want insert javascript this link on navigation bar, here code in layout file.
$useritems = []; if (yii::$app->user->isguest) { $useritems[] = [ 'label' => 'how works?', 'items' => [ ['label' => 'video', 'url' => ['/site/howto']], //i want insert javascript here ['label' => 'slide', 'url' => ['/site/index']], ], ]; $useritems[] = [ 'label' => 'support', 'items' => [ ['label' => 'faq', 'url' => ['/site/index']], ['label' => 'live chat', 'url' => ['/site/index']], ], ]; $useritems[] = [ 'label' => 'app store', 'url' => ['/site/index'] ]; } else { $useritems = menuhelper::getassignedmenu(yii::$app->user->id); } echo nav::widget([ 'options' => ['class' => 'navbar-nav navbar-left'], 'items' => $useritems, ]);
where should put code? can't find place put in /site/howto link
try this:
$useritems[] = [ 'label' => 'how works?', 'items' => [ ['label' => 'video', 'options' => [ 'onclick' => 'myfunction();', // give javascript function name ], 'url' => ['/site/howto']], ['label' => 'slide', 'url' => ['/site/index']], ], ];
implement function in javascript want
2nd way :
$useritems[] = [ 'label' => 'how works?', 'items' => [ ['label' => 'video', 'options' => [ 'class' => 'test', // give class name here ], 'url' => ['/site/howto']], ['label' => 'slide', 'url' => ['/site/index']], ], ];
in javascript :
$('.test').on('click', function(event){ // code });
Comments
Post a Comment