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 5 of 5

Thread: strings?

  1. #1
    Regular Coder
    Join Date
    May 2004
    Posts
    391
    Thanks
    76
    Thanked 0 Times in 0 Posts

    strings?

    If "key" holds the field name how do I insert key into "document.theForm.key.value"? Is what I did below correct?

    Code:
    document.theForm.+key+.value = value;

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Use document.getElementById(key).value instead.

    Example:-

    Code:
    <input type = "text" name = "txt1" id = "txt1" onblur = "chk()">
    
    <script type = "text/javascript">
    function chk() {
    var key = "txt1"
    var x = document.getElementById(key).value;
    }
    </script>

    “Expert: a man who makes three correct guesses consecutively.”
    Dr. Laurence J. Peter (American "hierarchiologist", Educator and Writer, 1919-1990)

  • Users who have thanked Philip M for this post:

    nikko50 (01-25-2009)

  • #3
    Regular Coder
    Join Date
    May 2004
    Posts
    391
    Thanks
    76
    Thanked 0 Times in 0 Posts
    So I can't reference by name?
    Tracy

  • #4
    Regular Coder
    Join Date
    May 2004
    Posts
    391
    Thanks
    76
    Thanked 0 Times in 0 Posts
    Thanks works great Philip!
    Tracy

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,349
    Thanks
    11
    Thanked 589 Times in 570 Posts
    Quote Originally Posted by nikko50 View Post
    So I can't reference by name?
    Tracy
    there is a document.getElementsByName()

    note that it returns a collection, not just a single tag like getById,
    so document.getElementsByName("txt1")[0] is probably how you would want to use it.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Posting Permissions

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