Messing with calling JS when onClick

Hi

I am trying to execute JS/Jquery code after user clicks on menu link. Some_dov should be shown. I’ve been messing with that for a while.

This is standard Yii-generated menu on the right with my trying:




$this->menu=array(

	array('label'=>'Create Model', 'url'=>array('create')),

   	array('label'=>'Manage Model', 'url'=>array('admin')),

	array('label'=>'Show all Model', 'url'=>array('index')),

	array('label'=>'Update this Model', 'url'=>array('update', 'id'=>$model->id)),

	array('label'=>'Execute_some_JS', 'url'=>'', 'onclick'=>array('show_hide()')),



Then below I have:




Yii::app()->clientScript->registerScript('show_hide', "

	$('#some_div').show('slow');

");



But it is not working with ‘url’=>’’, ‘onclick’=>array(‘show_hide()’) and with ‘url’=>’#’, ‘onclick’=>array(‘show_hide()’).

What’s wrong with that url and onClick?

Thanks for suggestions.

The first parameter to registerScript is an ID - ID that uniquely identifies this piece of JavaScript code it’s not the function name… you should write your function…

And you can always check what Yii generates with the browser view page or view source link…

In addition to what mdomba said I want to say that you have to change the way how you invoke your js function.

I’d do it like this


...

array('label'=>'Execute_some_JS', 'url'=>'#', 'linkOptions'=>array('onclick'=>'show_hide()')),

and


Yii::app()->clientScript->registerScript("s001","

        function show_hide()

	{

		$('#some_div').show('slow');

	}

",CClientScript::POS_HEAD);

This worked, thanks.

If you could provide this magic JQuery code which toggles URL ‘label’ from ‘Execute_some_JS_to_show_div’ to ‘Execute_some_JS_to_hide_div’ I would be grateful.

I am using JQuery’s toggle to play with that div.




Yii::app()->clientScript->registerScript("s001","

        function show_hide()

        {

                $('#some_div').toggle('fast');

        }

",CClientScript::POS_HEAD);



No I would like to toggle text of link depending of toggling div.

I tried to GooG13 and tried this




Yii::app()->clientScript->registerScript("s001","

function show_hide()

{

$('#some_div').toggle('fast');

$(this).text($(this).text() == 'Show Div' ? 'Hide div' : 'Show div');

}



But it is’nt changing link’s text.

Solved, works.

URL link must be this way:




array('label'=>'Show div', 'url'=>'#', 'linkOptions'=>array('onclick'=>'show_hide()', 'id'=>'show_some_div')),



and the code to show/hide div and toggle URL text is:




Yii::app()->clientScript->registerScript("s001","

function show_hide()

{

$('#some_div').toggle('fast');

$('#show_some_div').text($('#show_some_div').text() == 'Show Div' ? 'Hide div' : 'Show div');

}



Works as expected. Thanks everyone.