-
Javascript Alerts
My objective is to put some information from one field into another field as the user types the characters. But I'm stuck at step one. At the moment I'm just trying to get the information from the first field (and then I'll try and work out how to put it into the second field) but the code below keeps showing a blank message box after I've typed 1 character.
Code:
<!DOCTYPE html>
<html>
<head>
<title>TRIALJS</title>
<script>
function myFunction()
{
var valone = document.getElementById('txtone').value;
alert(valone);
}
</script>
</head>
<body>
<p>Some text.</p>
<input type="text" id="txtone" onkeydown="myFunction()">
<br><br>
<input type="text" id="txttwo">
</body>
</html>
So then I tried (looking for a non-blank field)...
Code:
<!DOCTYPE html>
<html>
<head>
<title>TRIALJS</title>
<script>
function myFunction()
{
var valone = document.getElementById('txtone').value;
if valone != ""
alert(valone);
}
</script>
</head>
<body>
<p>Some text.</p>
<input type="text" id="txtone" onkeydown="myFunction()">
<br><br>
<input type="text" id="txttwo">
</body>
</html>
But this doesn't even create a message box. So what can I do to fix this? Thank you.
-
Re: Javascript Alerts
The syntax for an if statement is:
Code:
if(condition)
statement;
So try this way:
Code:
if (valone != "")
alert(valone);
:wave:
-
Re: Javascript Alerts
Thanks for the code but unfortunately it didn't solve my problem. Without the if statement I'm still getting a blank messagebox (after I type the 1st character.) Then, when I type the 2nd character, I get a messagebox that shows me only the 1st character I typed.
So does anyone know why javascript shows a blank textbox after the 1st character has been typed? Thanks!
-
Re: Javascript Alerts
Here, I am using the KeyUp event to trigger the function, that will copies the contents of the first inputbox to the second one.
HTML Code:
<!DOCTYPE html>
<html>
<head>
<title>TRIALJS</title>
<script>
function myFunction()
{
var valone = document.getElementById('txtone').value;
document.getElementById('txttwo').value=valone;
}
</script>
</head>
<body>
<p>Some text.</p>
<input type="text" id="txtone" onkeyup="myFunction()">
<br><br>
<input type="text" id="txttwo">
</body>
</html>
Live: http://jsfiddle.net/ykter/
:wave:
-
Re: Javascript Alerts
Thanks again for the code akhileshbc, it's exactly what I needed (if I can I'll rep you some points.)
It'd still be good to know why I'm getting a blank messagebox though, so if anyone wants to offer an answer that would be great.
-
Re: Javascript Alerts
On my vs2008 it works fine
Code:
<script type="text/javascript">
function myFunction() {
var valone = document.getElementById('txtone').value;
document.getElementById('txttwo').value = valone;
alert(valone);
}
</script>