• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get one form_input to show up in another?

#1
Hi there,

I am just wondering if there is a way of having a non-usable input section show up what someone is typing in another input?

Thanks,

Doomie
Reply

#2
If you want this in real time you would probably want to use javascript.
Reply

#3
I have found a couple but none of them seems to want to work with my form  Huh
Reply

#4
jQuery is your best friend.
Load the jQuery library in your page's head section.
Then, at the bottom of your page (preferrably after the </body> tag):
Code:
<script>
$(document).ready(function() {
  $('#input1').keyup(function() {
      var text = $('#input1').val();
      $('#input2').val(text);
  });
});
</script>
In this example, "input1" is the id attribute of the input where the user is typing text, and "input2" is the id of the second field where you want to show the text that is being typed.
More info: https://api.jquery.com/keyup/
Reply

#5
I have been trying to mess around to get this to work.  The following 2 lines are the part of the comlpete form that I want it to copy to.

PHP Code:
<?php echo form_input(array('id' => 'input1''name' => 'name''class' => 'form-control'),'' $js); ?>
<?php 
echo form_input(array('id' => 'input2''name' => 'url''class' => 'form-control'),'' $js); ?>

I have added the code in the above post and added jquery and it just seems to do nothing and I cannot see why.
Reply

#6
You didn't describe the value of the $js variable, but in general, jQuery can handle elements by their id or class.
It should work if you do this:

PHP Code:
<?php echo form_input(array('id' => 'input1''name' => 'name''class' => 'form-control')); ?>
<?php 
echo form_input(array('id' => 'input2''name' => 'url''class' => 'form-control')); ?>

<script>
 $(document).ready(function() {
   $('#input1').keyup(function() {
       var text = $('#input1').val();
       $('#input2').val(text);
   });
 });
 </script> 
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.