I am generating a checkbox using the corresponding method of CHtml, and I want to run some JavaScript code before and after the AJAX request. Here is the code:
echo CHtml::checkBox('markComplete', FALSE,
array(
'class' => 'markComplete',
'ajax' => array(
'type' => 'POST',
'url' => $this->createUrl('/events/events/MarkComplete'),
'data' => 'event_status='.$events['id'],
'beforeSend' => 'function(){ $(this).parent("TR").hide(); }',
'success' => 'function(resp) { $("#right").append(resp); }'
),
)
);
How can I tell Yii that beforeSend and success are JavaScript code and not plain strings?
Better option available starting from Yii 1.1.11
All framework classes now support custom JavaScript snippets through
CJavaScriptExpression. Use it like this:The option of prefixing strings with
js:is still available by default, but it can now be disabled as required using an optional parameter onCJavaScript::encode.Original answer
If you want to include literal JavaScript code as part of options, the convention in Yii is to prefix the code with
js:. So you would write it like this:Unfortunately this is not well documented, which is why people run into exactly this problem every now and then.