Setting Focus to activeTextField

Hello all,

Wondering if there is a way to set focus to an activeTextField.  In Prado, I just did something like $this->field->focus(), but this doesn't work with Yii.

Any help appreciated.


You need to register a small piece of jquery code. It’s very easy. ;)

Well, "very easy" is a relative term Qiang.  I've looked through the Yii documentation and there isn't anything about how to use Jquery with Yii.  I see references to how the framework is included with Yii, but that's it.  Is there such a section?  If so, point me to it and I would be happy to read it.  If not, how are we expected to figure out how Yii and Jquery fit together? 

I have only used bits and pieces of Jquery with html documents.  There is a simple protocol to follow when embedding it within html, but it doesn’t seem to be the same within Yii.  Meaning, I don’t think statements such as <script type=“text/javascript” src=“jquery.js”></script> are required with the view file, but instead something along the lines of:

<?php Yii::app()->clientScript->registerScript(null,’$(”#what”).focus();’) ?>

Am I on the right track?  In the example above, #what refers to the name of the activeTextField.  It still doesn't seem to work.





Yii::app()->clientScript->registerScript('something', '$("#id").focus();');

where 'id' refers to the element ID that you want to set focus.

Thanks Qiang, but it still doesn't work. 

What's the purpose of the script code id "something"?  Here is my code.  I can't see what is wrong.  The jquery code works fine if I create an html page, outside of yii,  with a text field and add the jquery code. 

<?php Yii::app()->clientScript->registerCoreScript('jquery'); ?>

<?php Yii::app()->clientScript->registerScript('something', '$("#what").focus();') ?>

<?php echo CHtml::activeTextField($search,'what', array('size'=>35, 'maxlength'=>50, 'class'=>'bigTextBox')); ?>

For your activeTextField, you need the following:

<?php Yii::app()->clientScript->registerScript('something', '$("#'. CHtml::activeId($search,'what') . '").focus();') ?>

'something' can be anything that uniquely identifies this piece of code.

Basically, the goal is to determine the id attribute value of the input field. Here we use CHtml::activeId() to get this.

Thanks Qiang, that did the trick. 

Your help is greatly appreciated.



Thanks Qiang, i am also  waiting for this.

how to copy value of first textbox to second text box on blur of first textbox with disabling second textbox


What if it is a "textField" rather than an "activeTextField"?