How to write JQuery in view file ? !

Please, i will like to know how I can add JQuery code in my view file.

I will like to be able to type some Jquery functions directly into the view file without loosing

IDE code completion, syntax highlighting etc.

I once saw something like this:

<script>

$script = <<< JS

$(’#my_dropdown7’).mousedown(function(){

if($(this).options.length>8)

{

   &#036;(this).size=5;

}

alert(‘MOUSE DOWN !!!’);

});

</script>

Kindly help confirm the syntax and HOW & WHERE do i register the script !!

Thanks.

1 Like

http://www.yiiframework.com/doc-2.0/guide-output-client-scripts.html

Thanks @hrnair,

But I didn’t get much from your link above

I needed guidance as to how to add Jquery like so:

<script>

$script = <<< JS

$(’#my_dropdown7’).mousedown(function(){

if($(this).options.length>8)

{

$(this).size=5;

}

alert(‘MOUSE DOWN !!!’);

});

JS;

</script>

<?php

//$this->registerJs($script, View::POS_END);

?>

Or Rather

Someone should please correct the above script !!

Thanks.

Hi,


$script = "$('#my_dropdown7').mousedown(function(){

 if($(this).options.length><img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' />

 {

 $(this).size=5;

 }

 alert('MOUSE DOWN !!!');

 });"



just add $this->registerJs($script); POS_END is default.

See the source Html to see how this is rendered.

1 Like