Active Form Text Field To Text Field On Key Press

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

Hi there. Can you post the code that’s not working?

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






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

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