1

I have two inputs fields:

<input type="text" id="one" name="one" />
<input type="text" id="two" name="two" />

I want a function to copy the text of the first input automatically when we click on the second input without using Jquery

Thanks

3 Answers 3

1

To check if the user clicks on the <input> element, add an event listener to it.
Then, get the value of the first text field using the value property.

Here is your code:

document.getElementById('two').addEventListener("click", function() {
  this.value = document.getElementById('one').value;
});
<input type="text" id="one" name="one" />
<input type="text" id="two" name="two" />

Here is a living demo: https://codepen.io/marchmello/pen/XWmezNV?editors=1010

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

1 Comment

Note, the demo shows that the second field doesn't update until it itself is clicked.
1

A basic way of doing this:

<input type="text" id="one" name="one">
<input type="text" id="two" name="two" onfocus="this.value = document.getElementById('one').value">

Comments

0

here is the example to do this.

var one = document.getElementById("one");
var two = document.getElementById("two");
function myFunction(){
two.value = one.value;
}
<input type="text" id="one" name="one" />
<input type="text" id="two" name="two" onfocus="myFunction()" />

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.