View Full Version : Move last X chars from one field to another

03-24-2008, 06:09 PM
Hello helpful people! :)

I have been browsing the forum a while and decided this place is so friendly that I will try my luck submiting my own question.

Here goes:

I need a javascript that can take all characters after the first X amount entered into a text field and move them to another when the user exits the field (using onblur?).

Kudos to anybody who can help! :)

03-24-2008, 06:26 PM
<textarea onBlur="document.getElementById('receive').value=this.value.substring(3)"></textarea>
<textarea id="receive"></textarea>
where 3 is the number of characters. Can work with input too, you just need the id.

03-24-2008, 06:55 PM
Excellent, Thanks!

That was perfect!

I forgot to say I needed the X characters removed from the first field as well as placed in the second field.

Any idea how to do that?

Thanks again for such a speedy response!

03-24-2008, 09:54 PM
you want the characters removed in the first field and the text from the first, in the second?

<textarea onBlur="document.getElementById('receive').value=this.value.substring(3);this.value=this.value.substring(3)"></textarea>
<textarea id="receive"></textarea>

03-25-2008, 07:41 AM
Thanks, but not quite like that.

I need the text that is moved to the second field removed from the first, while the beginning of the text remains.

For example, if I write "Hello World" in the first field, with 3 as the number to keep, then "Hel" will stay in the first, and "lo World" will be moved to the second field.

I hope that makes it clearer.

Kudos to whomever can help me out! :)

03-25-2008, 08:06 AM
See if this code fit your heart's desire ;)
I'll separate the presentation (JS) from the markups (html).

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
window.onload = function()
document.getElementById('input').onblur = function()
<textarea id="input"></textarea>
<textarea id="receive"></textarea>

03-25-2008, 08:36 AM
Damn, you're good!

That's EXACTLY what I needed.

Thanks rangana! :)