nareboy
(Felipe Moreira)
April 17, 2012, 3:15pm
1
Hi guys.
I searched a lot on internet to solve this simple problem but could not get results. Then I would ask for help from you.I simply want to add a javascript function that runs when the onkeydown or onkeypress event is fired, but I do not want this moment to store this function in the file, I only run a javascript function directly.
Example
<? php echo $ form-> textField ($ model, ‘local’, array
(
‘onkeyup’ => ‘myfunctionjs();’
)
And later I would use functions in events that are stored in separate javascript.
How can I do this?
Thank You!
saegeek
(Abdallah)
April 17, 2012, 7:33pm
2
It’s better to NOT add attributes like onkueyup etc. in the CForm element but just an id :
<input id="foo">
Then (in the view) you register the script as follows :
(so Yii will auto-insert jQuery.js between <head> and </head>)
Yii::app()->getClientScript()->registerScript('myscript','$("#foo").keyup(function() {
alert(\'Hello world !\');
});');
More events here:
http://api.jquery.com/category/events/keyboard-events/
nareboy
(Felipe Moreira)
April 17, 2012, 10:28pm
3
I did the following in my view:
Yii::app()->getClientScript()->registerScript(‘myscript’,’$("#foo ").keyup(function() {
alert(\‘Hello world !\’);
});’);
<div class="row" id="foo">
<?php echo $form->labelEx($model,'observacao'); ?>
<?php echo $form->textArea($model,'observacao'); ?>
<?php echo $form->error($model,'observacao'); ?>
</div>
and it worked! But suppose I have more than one element within a div, how I would associate an id to the target element? And every time I want to handle an event I must assign an id to the div element that contains the target element? There is another way?
Thank you so much.
saegeek:
It’s better to NOT add attributes like onkueyup etc. in the CForm element but just an id :
<input id="foo">
Then (in the view) you register the script as follows :
(so Yii will auto-insert jQuery.js between <head> and </head>)
Yii::app()->getClientScript()->registerScript('myscript','$("#foo").keyup(function() {
alert(\'Hello world !\');
});');
More events here:
http://api.jquery.com/category/events/keyboard-events/
nareboy
(Felipe Moreira)
April 18, 2012, 12:02am
4
I did the following:
<?php echo $form->checkBox($model,‘justificar’,array
(
‘id’=>‘chk_just’
)
); ?>
now I have the id of the element to handle any events.
Thank you very much.
ejaz
(Ejita Karim)
August 22, 2013, 11:16am
5
thanks … this post helped me too