text to other text field

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

text to other text field

Post by shiznatix »

Original question:

what i want is what a user puts in text field A to go to text field B as soon as the user clicks away or if it was instant that would be cool too. what would be the JS for this?

Responce I got:

Well you can make it so that after so many characters it goes in there like so: (Lets say 8 chars for this example)

Code: Select all

<script type="text/javascript">
<!--
function switchFocus(num, fromNode, toEl)
{
    if (fromEl.value.length >= num) document.getElementById(toEl).focus();
}
// -->
</script>

A: <input type="text" id="A" name="A" onkeypress="switchFocus(8, this, 'B');" /> <input type="text" id="B" name="B" />
Or you can make it hop across when the user clicks away (although that sounds silly since they'd most likely either be "tabbing" to the other box or using the mouse but anyway:

Code: Select all

<script type="text/javascript">
<!--
function switchFocus(toEl)
{
   document.getElementById(toEl).focus();
}
// -->
</script>

A: <input type="text" id="A" name="A" onblur="switchFocus('B');" /> <input type="text" id="B" name="B" />
my new reply now
i don't want the cursor to go to the new text field, what i want is the text that was entered into the first text box to be coppied to the 2nd one onblur probably or if it could be done in real time that would be great. the 1st part is at the top of the page then i just want as they scroll down the text will have been copied into the textbox at the bottom.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

Code: Select all

function copyIt()
{
   var field1 = document.getElementById('fieldone');
   var field2 = document.getElementbyId('fieldtwo');
   field2.value = field1.value;
}

<input type="text" name="fieldone" id="fieldone" onblur="copyIt()">
<input type="text" name="fieldtwo" id="fieldtwo">
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

Code: Select all

function copyIt()
{
    document.getElementById('fieldone') = document.getElementbyId('fieldtwo');
}

<input type="text" name="fieldone" id="fieldone" onblur="copyIt()">
<input type="text" name="fieldtwo" id="fieldtwo">
worked perfect. thanks kiddies
Post Reply