Results 1 to 6 of 6

Thread: Javascript Alerts

Hybrid View

  1. #1

    Thread Starter
    Lively Member Yumby's Avatar
    Join Date
    Feb 2009
    Posts
    120

    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.

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    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,...

  3. #3

    Thread Starter
    Lively Member Yumby's Avatar
    Join Date
    Feb 2009
    Posts
    120

    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!

  4. #4
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    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,...

  5. #5

    Thread Starter
    Lively Member Yumby's Avatar
    Join Date
    Feb 2009
    Posts
    120

    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.

  6. #6
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    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
  •  



Click Here to Expand Forum to Full Width