Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-26-2011, 11:44 AM   PM User | #1
chonji
New to the CF scene

 
Join Date: Sep 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
chonji is an unknown quantity at this point
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>
chonji is offline   Reply With Quote
Old 09-26-2011, 12:40 PM   PM User | #2
blaze4218
Regular Coder

 
Join Date: Apr 2005
Location: Texas
Posts: 448
Thanks: 24
Thanked 63 Times in 63 Posts
blaze4218 is an unknown quantity at this point
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
blaze4218 is offline   Reply With Quote
Old 09-26-2011, 01:20 PM   PM User | #3
chonji
New to the CF scene

 
Join Date: Sep 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
chonji is an unknown quantity at this point
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);
chonji is offline   Reply With Quote
Old 09-26-2011, 01:32 PM   PM User | #4
blaze4218
Regular Coder

 
Join Date: Apr 2005
Location: Texas
Posts: 448
Thanks: 24
Thanked 63 Times in 63 Posts
blaze4218 is an unknown quantity at this point
yes
Code:
document.getElementById('formField').value = myStr;

Last edited by blaze4218; 09-26-2011 at 02:34 PM.. Reason: forgot the getElement :o
blaze4218 is offline   Reply With Quote
Old 09-26-2011, 02:18 PM   PM User | #5
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 806
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
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>
DaveyErwin is offline   Reply With Quote
Old 09-26-2011, 03:04 PM   PM User | #6
chonji
New to the CF scene

 
Join Date: Sep 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
chonji is an unknown quantity at this point
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
chonji is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:37 AM.


Advertisement
Log in to turn off these ads.