2

Is it possible to copy an input from a previously entered field into another uneditable field javascript? Say for example you have two name fields, the first field you must enter your name, once entered the name will automatically be copied in the second text field which is uneditable. I have done a bit of searching around but i can't seem to find anything of use for this specific situation and i'm quite knew to javascript so any sort of help or a nudge in the right direction would be great.

5
  • What do you mean "uneditable"? As in, it has a readonly attribute? Get the elements by id and get/set the appropriate value property of them Commented May 21, 2013 at 16:38
  • If you have 2 id's , one for each text field , you can do document.getElementById('inputone').value = document.getElementById('inputtwo').value Commented May 21, 2013 at 16:39
  • 1
    It's possible. And easy with jQuery jsfiddle.net/j08691/WFvU9 Commented May 21, 2013 at 16:39
  • @Ian yes! sorry readonly is the word im looking for. Commented May 21, 2013 at 16:45
  • @j08691 that seems to work fine in jsfiddle but when i try to add it to my own code it doesn't work, should be between something? Commented May 21, 2013 at 16:46

5 Answers 5

3

Just use the keyup listener of your input:

<input type="text" id="input1" />
<input type="text" id="input2" readonly='readonly' />

var $input2 = document.getElementById('input2');
var $input1 = document.getElementById('input1');
$input1.addEventListener('keyup', function()
{
  $input2.value = $input1.value;
});

here's example: fiddle

Sign up to request clarification or add additional context in comments.

2 Comments

It works find in jsfiddle but when i try it in my own code it doesn't work, should it be add between something?
oh wait i got it working, just added window.onload = function(event), thank you very much for the help!
3

Yes you could, just do the follow:

document.getElementById('non_editable_name').value = document.getElementById('editable_name').value;

if you use jquery, you can do:

$('#non_editable_name').val($('#editable_name').val());

Comments

0

Here is a fiddle. You can do this by using jQuery and code:

$(function () {
    $('#nameInput').change(function () {
        $('#secondInput').val($(this).val());
    });
});

with:

<input id="nameInput" type="text"/><br />
<input id="secondInput" type="text" readonly/>

1 Comment

You're assuming the OP is using jQuery.
-1

HTML

<input type="text" id="entry" />
<input type="hidden" id="storage" />

JS

(function() {

    $('#entry').on('change', function() { $('#storage').val(this.value); });

})();

1 Comment

Assuming that the OP is using jQuery. Also , what's the point of a 'hidden` input field?
-1

Like this:

   <!DOCTYPE html>
    <html>
      <head>
      <meta http-equiv="content-type" content="text/html; charset=windows-1250">
      <meta name="generator" content="PSPad editor, www.pspad.com">
      <title></title>
      <script>
      function copianome(nome){
        document.getElementById('ds_name_result').value = nome;
      }
      </script>
      </head>
      <body>
        <input type="text" id="ds_name" onkeyup="copianome(this.value)" /><br />
        <input type="text" id="ds_name_result" readonly="readonly" />
      </body>
    </html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.