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 6 of 6
  1. #1
    Banned
    Join Date
    Jun 2012
    Posts
    81
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Inserting text on click

    I am trying to insert text into an INPUT field when I click the button "1".

    I have used this code, but its not working. Can anyone see what I have done wrong?

    Code:
    <INPUT type="button" value="1" onclick="document.forms["form"]["number"].value .= "1"">

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Code:
    onclick="document.forms["form"]["number"].value .= '1'"
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    Phalanxer (06-28-2012)

  • #3
    Banned
    Join Date
    Jun 2012
    Posts
    81
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Thanks for that! ... Its been one of thoughs days!!

    I also changed the double quotes to quotes, as this was causing an error too, now my error console reports the following error:

    Error: missing name after . operator
    Source Code:
    document.forms['form']['number'].value .= '1'
    I'm not quite sure what to make of this. Do you have any idea what it means?
    Last edited by Phalanxer; 06-28-2012 at 10:57 AM.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,927
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Phalanxer View Post

    I'm not quite sure what to make of this. Do you have any idea what it means?
    It means a typo - should be

    onclick="document.forms['form']['number'].value = '1'";

    '1' is a string value. 1 is a number.

    It is not a good idea to assign the name number to a form field. Use num or something.

    You have now spotted that you were using double quotes within double quotes.

    Its been one of thoughs days!!
    Last edited by Philip M; 06-28-2012 at 11:01 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Banned
    Join Date
    Jun 2012
    Posts
    81
    Thanks
    7
    Thanked 0 Times in 0 Posts
    I can't use:

    Code:
    onclick="document.forms['form']['number'].value = '1'"
    ... because if I press my virtual buttons '1' '2' and '3', then the INPUT number is going to be '3', where I need it '123', hence why I tried using .=.
    Last edited by Phalanxer; 06-28-2012 at 11:07 AM.

  • #6
    Banned
    Join Date
    Jun 2012
    Posts
    81
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Ah, I just needed to change .= to +=.

    Solved.


  •  

    Posting Permissions

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