View Full Version : Accessing value of string variable in assignment

09-26-2011, 12:44 PM
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?

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

09-26-2011, 01:40 PM
Your almost there

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

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

09-26-2011, 02:20 PM
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:

...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:

string myStr = "first name";


09-26-2011, 02:32 PM

document.getElementById('formField').value = myStr;

09-26-2011, 03:18 PM
<script type="text/javascript">
var nameVal = "Hiyas";
Name: <br><input type = "text"
name = "nameField"
id = "idField"
size = "50"
onChange ="this.value=storeVal(this.value, '0')">
<script type="text/javascript">

09-26-2011, 04:04 PM
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

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum