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
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Accessing value of string variable in assignment

    Hi All,

    Am new to Javascript but fluent in C++ and other languages, I have a query, i made a global variable to hold string information, can the value of this string be assigned to a form input field value?

    I mean in the 'default value sense' I am just wondering if there is any syntax to accomplish this.

    EG in my javascript i have the string nameVal; It is a global variable, i would like to know if it is poosible access its value for use in the line below shown with italics, or is it basically always a case of 'type the value in manually yourself' and then rely on events functions to update the field thereafter?

    Code:
    Name: <br><input type = "text" name = "nameField" size = "50" value = my string val here onChange ="this.value=storeVal(this.value, '0')"></input></br>

  • #2
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    Your almost there
    Code:
    Name: <br><input type = "text" name = "nameField" size = "50" value ="my string val here" onChange ="this.value=storeVal" /></br>
    end the Input tag with /> instead of a closing </input>
    Also, if it is necessary to choose a sub string from a given index of storeVal you could always try
    Code:
    Name: <br><input type = "text" name = "nameField" size = "50" value ="my string val here" onChange ="this.value=storeVal.substr(0,storeVal.length)"></br>
    where the first argument of substr() is the starting index, and the second argument is the ending index

  • #3
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    wrong value

    Hi Thanks for the reply and useful advice, I don't think i explained myself properly though, i understand how to assign the value using a string literal = " text..." and also the use of the java function called with an inChange or onClick event to set the field value, but my query was relating to the initial value assign statement:

    Code:
    ...value = my string val here ...
    relating to the optional value parameter to use at the fields construction
    i would like to know if it is poossible to replace the words 'my string val here'
    with a variable name, thus assigining the value of the field to the value of the variable

    like in C++ you might have something like:

    Code:
    string myStr = "first name";
    
    formField.value(myStr);

  • #4
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    yes
    Code:
    document.getElementById('formField').value = myStr;
    Last edited by blaze4218; 09-26-2011 at 02:34 PM. Reason: forgot the getElement :o

  • #5
    Regular Coder
    Join Date
    Aug 2010
    Posts
    970
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Code:
    <html>
    <head>
    <script type="text/javascript">
    var nameVal = "Hiyas";
    </script>
    </head>
    <body>
    Name: <br><input type = "text" 
    name = "nameField" 
    id = "idField"
    size = "50" 
     onChange ="this.value=storeVal(this.value, '0')">
    </input></br>
    <script type="text/javascript">
    document.getElementById("idField").value=nameVal;
    </script>
    </body>

  • #6
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    ta

    Thanks all for the contributions, i have this one worked out now, another point is in need of some attention though - i have created a new thread,

    It is regarding refilling my form fields after the mouse loses focus


  •  

    Posting Permissions

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