By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,928 Members | 1,590 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,928 IT Pros & Developers. It's quick & easy.

JavaScript autopopulate a text box

P: 8
I have a JSP page which has two text boxes.
I would like to be able to enter a number into the first textbox, and to have that number automatically appear in the second textbox multiplied by 3.
So if I enter number 1 into the first text box, then I want number 3 appear in the second text box.
Does anyone know how to do that? Do I need to use JavaScript for that?

Thank you!
Oct 25 '07 #1
Share this Question
Share on Google+
8 Replies


Ferris
100+
P: 101
hi

yes,you do need javascript...

here's the code I write for you:


[HTML]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<script language="javascript">
function mul()
{
var a = document.getElementById("first").value;
document.getElementById("second").setAttribute("va lue",a * 3);
}
</script>
<body>
<input type="text" id="first" onChange="mul();" value="0" />
<input type="text" id="second" value="0" />
</body>
</html>


[/HTML]
Oct 25 '07 #2

P: 8
Ferris,
Thank you for such a quick reply!!! You code works good, but right now after typing a number in my first text box - I have to click somewhere or hit TAB, Only then the second text box is multiplied by three.
Is there a way to have the number in second text box change without me leaving the first box?

Kind regards.
Oct 25 '07 #3

Ferris
100+
P: 101
hi

change line 16 :
<input type="text" id="first" onChange="mul();" value="0" />

into

<input type="text" id="first" onKeyUp="mul();" value="0" />
Oct 25 '07 #4

P: 8
Ferris,
This works great!!!

Thank you!!!
Oct 25 '07 #5

helimeef
P: 77
hi

change line 16 :
<input type="text" id="first" onChange="mul();" value="0" />

into

<input type="text" id="first" onKeyUp="mul();" value="0" />
Don't do that Ferris! IE doesn't support key events, use a setInterval every few seconds to fill er up.
Oct 26 '07 #6

Ferris
100+
P: 101
Don't do that Ferris! IE doesn't support key events, use a setInterval every few seconds to fill er up.

thanks for telling me that,but I have tested my code in both IE and Firefox,they works well.
Oct 26 '07 #7

acoder
Expert Mod 15k+
P: 16,027
Don't do that Ferris! IE doesn't support key events, use a setInterval every few seconds to fill er up.
What makes you say that? Can you give an example that doesn't work?
Oct 29 '07 #8

helimeef
P: 77
What makes you say that? Can you give an example that doesn't work?
Sorry I guess it does. I think it's IE6 that doesn't support. Maybe just starting to have nightmares about IE :p
Dec 28 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.