JQuery Ajax request onclick

Hi @ all !

I would like to do an ajax request.

I saw how about making a such query in the cookbook but I’m wondering how I could tell a such code by an onclick event like in :

<input type="button" onclick="Ajax('foobar');">

Using a code like :

<?php echo CHtml::script(CHtml::ajax(array('url'=>'/ajax',




							'data'=>'({title : this.getAttribute(\'id\')})'


I get :

<script type="text/javascript">


jQuery.ajax({'url':'/ajax','type':'POST','dataType':'html','data':'({title : this.getAttribute(\'id\')})','cache':false});



Should I have to use something like :

<input type="button" onclick="jQuery.ajax();">

After some forum search, I finally found how to do.

I had to call CHtml::ajaxLink() or CHtml::ajaxButton() functions like in :

echo CHtml::ajaxLink('#',bu().'/ajax/titles',






'beforeSend' => 'function(){$("#title").replaceWith(\'<img id="title" alt="Veuillez patienter..." src="'.bu().'/images/loading.gif">\');}',



But I had to NOT use the Yii “replace” keyword because it wouldn’t replace the target HTML element (here with id=“title”) but have to use manually a ‘success’ tag.

oops, it was a syntax error, i found how to use the ‘replace’ tag

example for :

<input id="title">

I had to do a :