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
    May 2009
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Join two input values into one value in html or javascript codes?

    hi, can somebody answer my question? i want an html code or javascript equivalent for this particular case.
    i want to put two input values into one.in other words, join them together..example:

    <input name="name" value="William"/>
    <input name="surname" value="Shakespeare"/>

    and then the next input would be the the combination of the first two input

    example:

    input name="completename" value="(name + surname)"

    to yield and input value of value="William Shakespeare"

    thank you so much.any reply would be a great help.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Try this:-

    Code:
    <form name = "myform">
    <input name="firstname" value="William" onblur = "combine()"><br>
    <input name="surname" value="Shakespeare" onblur="combine()"><br>
    <input name="completename"  readonly=readonly<br>
    </form>
    
    <script type = "text/javascript">
    function combine() {
    var fn = document.myform.firstname.value;
    var sn = document.myform.surname.value;
    var cn = fn + " " + sn;
    document.myform.completename.value = cn;
    }
    </script>
    NB You should avoid giving an HTML element the name = name! Make it firstname.


    He thought he saw an Elephant,
    That practised on a fife:
    He looked again, and found it was
    A letter from his wife.
    "At length I realise," he said,
    "The bitterness of Life!"
    - Lewis Carroll
    Last edited by Philip M; 05-16-2009 at 06:04 PM. Reason: Improved

  • Users who have thanked Philip M for this post:

    dadynidhen (05-17-2009)

  • #3
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,091
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb Try this ...

    See if this will work for you:
    Code:
    <html>
    <head>
    <title>Input Joining</title>
    <script type="text/javascript">
    // From: http://www.codingforums.com/showthread.php?t=166484
    
    function FillIn() {
      document.getElementById('fullname').value = 
        document.getElementById('fname').value + ' ' + document.getElementById('surname').value; 
    }
    </script>
    </head>
    <body onLoad="FillIn()">
    <input id="fname" name="fname" value="William" onblur="FillIn()" />
    <input id="surname" name="surname" value="Shakespeare" onblur="FillIn()" />
    <input id="fullname" name="fullname" value="" />
    </body>
    </html>


    It's not fair that 'PhillipM' types faster than me ... my Native American name is "TypesToSlow".
    Last edited by jmrker; 05-16-2009 at 05:06 PM. Reason: Types too slow

  • Users who have thanked jmrker for this post:

    dadynidhen (05-17-2009)

  • #4
    New to the CF scene
    Join Date
    May 2009
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts
    hey both of you are great,,i use your codes and it helps me a lot..both of your codes work!!!again thank you philip m and jmrker


  •  

    Posting Permissions

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