0

I wanted to pass the value of input field to another input field with its value via javascript. I wrote code as shown below.

First input:

<input type="text" class="form-control" id="recipient" name="recipientName" value="RecipientName" onkeyup="recipient()"/>

Second input:

<input type="hidden" id="recipientHidden" name="recipientName"/>

Js Code

function recipient(){
   var recipientNameValue = document.getElementById('recipient').value;
   document.getElementById('recipientHidden').value = recipientNameValue;
   console.log(document.getElementById('recipientHidden').value);
}

When I open console, there is no value in the console. When I click on the first input field, value is printed.

How can I get it instantly ?

1
  • 2
    Your code works flawlessly over here. Did you get an error in the console? Commented Jun 12, 2019 at 10:33

1 Answer 1

1

Your code works fine, but it will only log the value on key up. You can log the value immediately by calling the recipient() function right away:

function recipient() {
  var recipientNameValue = document.getElementById('recipient').value;
  document.getElementById('recipientHidden').value = recipientNameValue;
  console.log(document.getElementById('recipientHidden').value);
}

// Call function straight away
recipient()
<input type="text" class="form-control" id="recipient" name="recipientName" value="RecipientName" onkeyup="recipient()" />

<input type="hidden" id="recipientHidden" name="recipientName" />

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

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.