click event

Hi!

I have a link (with image).

I need to make some ajax call on click.

However, I cannot even register simple click event.

Where is my mistake?




<?php 

Yii::app()->clientScript->registerScript('tester',

	'$(".down-link").bind("click",function(){alert("test");})'); //this never happens!!!

	

$this->widget('bootstrap.widgets.BootGridView',array(

	'id'=>'nav-grid-view',

	'type'=>'striped bordered condensed',

	'dataProvider'=>$dataProvider,

	'template'=>"{items}",

	'columns'=>array(		

		'id',

		'code',	

		'title',	

		'nav_order',

		'is_active',

		array(

            'class'=>'bootstrap.widgets.BootButtonColumn',

            'htmlOptions'=>array('style'=>'width: 50px'),

            'header'=>Yii::t('app','Operations'),

        ),

    array(

            'type'=>'raw',

            'name'=>'down',

            'value'=>

            	'CHtml::link(CHtml::image("images/up/test.png"), 

            	"#",

         			array(

           			"id" => "down-link-{$data["id"]}",

           			"class" => "down-link"

         			))',

            'sortable'=>false,

    ),    

	), //this is link on which i want to register event	

)); 

?>



Any suggestions… Stil do not see problem why a button doesn’t rise click event.

Looks like you’re binding the click event to the element before it has been rendered by the DOM.


Yii::app()->clientScript->registerScript('tester',

        '$(".down-link").bind("click",function(){alert("test");})'); //this never happens!!!

Try moving this below the widget so the binding occurs after the widget has been rendered.

To move it down (make it to register below <body> tag) why not use the CClientScript::POS_END constant.

You may put it as the third parameter in


Yii::app()->clientScript->registerScript('tester',

        '$(".down-link").bind("click",function(){alert("test");})'); 

So, it’ll become like this:


Yii::app()->clientScript->registerScript('tester',

        '$(".down-link").bind("click",function(){alert("test");})', CClientScript::POS_END); 

FYI: the default parameter is POS_HEAD. Read more here.