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 Answers
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
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
j08691
You're assuming the OP is using jQuery.
HTML
<input type="text" id="entry" />
<input type="hidden" id="storage" />
JS
(function() {
$('#entry').on('change', function() { $('#storage').val(this.value); });
})();
1 Comment
Harsha Venkataramu
Assuming that the OP is using
jQuery. Also , what's the point of a 'hidden` input field?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>
readonlyattribute? Get the elements byidand get/set the appropriatevalueproperty of them