Welcome to WebmasterWorld Guest from 174.129.96.175

Forum Moderators: open

variables in javascript not recognised?

What am i doing wrong

   
2:17 pm on Feb 16, 2009 (gmt 0)

5+ Year Member



How does javascript deal with variables? In this statement 'document.formname.fieldname.value' i cannot use variables?

This does not work
function ajaxFunction(_formname,_fieldname){
//simple code here to set var xmlHttp;
xmlHttp.onreadystatechange=function() {
if(xmlHttp.readyState==4) {
document._formname._fieldname.value=xmlHttp.responseText;
}
}
//open and send statements here
}

This does work
function ajaxFunction(_formname,_fieldname){
//simple code here to set var xmlHttp;
xmlHttp.onreadystatechange=function() {
if(xmlHttp.readyState==4) {
document.contact_name.name.value=xmlHttp.responseText;
}
}
//open and send statements here
}

Checking of values
When i alert(_formname+' '+_fieldname); i do get 'contact_name name' in a popup.

3:54 pm on Feb 16, 2009 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Try this method:

document[_formname][_fieldname].value = xmlHttp.responseText;

5:05 pm on Feb 16, 2009 (gmt 0)

5+ Year Member



HAH! Brilliant.Thanks a lot! Works like a charm.
Do you know why i works the way you wrote it and not without the brackets?
10:27 am on Feb 27, 2009 (gmt 0)

5+ Year Member



thats how you write arrays, in square brackets
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month