-
Nov 24th, 2012, 09:40 PM
#1
Thread Starter
Lively Member
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.
-
Nov 24th, 2012, 10:43 PM
#2
Re: Javascript Alerts
The syntax for an if statement is:
Code:
if(condition)
statement;
So try this way:
Code:
if (valone != "")
alert(valone);
If my post was helpful to you, then express your gratitude using Rate this Post.
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Nov 25th, 2012, 02:46 AM
#3
Thread Starter
Lively Member
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!
-
Nov 25th, 2012, 03:29 AM
#4
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/
If my post was helpful to you, then express your gratitude using Rate this Post.
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Nov 25th, 2012, 05:09 AM
#5
Thread Starter
Lively Member
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.
-
Nov 27th, 2012, 11:08 PM
#6
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>
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|