Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Onclick question

    My situation is that I want my script to:

    a)detect if the field is empty (no problem)
    b)if the field is empty to pull data from 2 other different fields and populate the empty one. (where I am having the problem)

    Can an onclick populate an empty field with 2 others from its own form and not overwrite it if it was already filled? If so, can someone show me an example of the script? I searched a bit and was not able to find an answer.

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Sure it can. It can call any function you like, or you can do it inline.
    I like functions.

    function foo()
    {
    // your code here
    }

    <yourelement onclick="foo();">

    How you set this all up depends on your form and all that jazz, so I can't show you too much without at least the html of your form.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #3
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,338
    Thanks
    29
    Thanked 282 Times in 276 Posts
    Quote Originally Posted by Typhoid Timmy View Post
    Can an onclick populate an empty field with 2 others from its own form and not overwrite it if it was already filled?
    I’m assuming that you’re asking whether or not you can add the contents of field A to the empty field then not have those contents overwritten when having the contents of field B appended to that same (previously empty) field. If so, that’s easy enough:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
      <head>
    
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <meta name="Author" content="Patrick Garies"/>
        <title>XHTML 1 Strict Document</title>
    
        <style type="text/css">
          html { padding: 1em; }
          body { margin: 0; }
          fieldset { padding: 0.5em; text-align: center; }
        </style>
    
        <script type="text/javascript">
          function combine() {
            var input = document.getElementsByTagName("input");
            if (!input[2].value) input[2].value = input[0].value + input[1].value;
            else alert("The input field is not empty.\nUnable to comply.");
            }
        </script>
    
      </head>
      <body>
    
        <fieldset>
          <input type="text" value="basket"/>
          <input type="text" value="ball"/>
          <input type="text"/>
          <input type="submit" value="Combine" onclick="combine();"/>
        </fieldset>
    
      </body>
    </html>
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #4
    New to the CF scene
    Join Date
    Dec 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That was exactly what I was looking for....thanks Arb!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •