jonnymaboy
(Jon Phsoftware)
October 22, 2012, 9:51pm
1
Hi all,
I have an activeform with multiple fields from my model. However what I have been trying is onkeypress of textfield1 populate textfield2 using jquery but I can’t get it to work. I need this to happen live clientside and be able to handle paste. Any help would be really appreciated.
Thanks Jon
abennouna
(Abennouna)
October 22, 2012, 10:45pm
2
Hi there. Can you post the code that’s not working?
jonnymaboy
(Jon Phsoftware)
October 23, 2012, 10:49am
3
Thanks or the reply. Thats the problem i dont know how to do it. I have tried lots of ways but none work 2 i have tried are below. Please can you exaplain the best way to do it thanks:
Example 1
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'form-grp',
'enableAjaxValidation'=>true,
'focus'=>array($model,'name'),
)); ?>
textbox1 (name)
<?php echo $form->textField($model,'name',array ('onKeyPress'=>'grp_url_alias.value=grp_name.value' )); ?>
textbox2 (url alias)
<?php echo $form->textField($model,'url_alias'); ?>
Example 2
<script type="text/javascript">
$(function(){
$("#grp_name").keypress(function()
{
$("#'grp_url_alias").val($(this).val());
}
});
</script>
Thanks
abennouna
(Abennouna)
October 23, 2012, 11:18am
4
I’m not sure here… Some thoughts:
I won’t say anything for inline JS (Example 1) I haven’t used it for a long time
For Example 2, I think selectors in jQuery are key sensitive, so you can try like this:
(provided that "Grp" is your model name)
<script type="text/javascript">
$(function(){
$("#Grp_name").keypress(function()
{
$("#Grp_url_alias").val($(this).val());
}
});
</script>
And anyway, try to debug your js code (add some stops, some alert() or console.log()) to see what happens really.
Also, make sure you don’t have any JS error!
jonnymaboy
(Jon Phsoftware)
October 23, 2012, 11:43am
5
I’m not sure here… Some thoughts:
I won’t say anything for inline JS (Example 1) I haven’t used it for a long time
For Example 2, I think selectors in jQuery are key sensitive, so you can try like this:
(provided that "Grp" is your model name)
<script type="text/javascript">
$(function(){
$("#Grp_name").keypress(function()
{
$("#Grp_url_alias").val($(this).val());
}
});
</script>
And anyway, try to debug your js code (add some stops, some alert() or console.log()) to see what happens really.
Also, make sure you don’t have any JS error!
Thanks for your help it works great. Just one more thing should i load the script via yii::registerscript or just chuck it in the view.
Thanks
jonnymaboy
(Jon Phsoftware)
October 23, 2012, 11:47am
6
Actually i have testted the below and can do all inline adding to htmloptions.
<?php echo $form->textField($model,'name',array ('onkeyup'=>'js: $(function() { $("#Grp_name").keyup(function() { $("#Grp_url_alias").val($(this).val()); })});')); ?>
great help thankyou