...

View Full Version : assigning a new variable problem?



Oziam
01-19-2011, 03:52 AM
Hi,
I am new to javascript and cannot figure this out, it is probably very simple.

I am trying to assign a new variable to write to a DIV,
I have the error messages set to variables like below;
the result is a pipe delimited string with the error and the field name,
e.g "error1|name" "error2|email" etc.... I have no problem retrieving
the result or splitting the string but I cannot seem to get the relevant
error message to display properly, it just displays the string "error1" or "error2" etc...


var error1 = "name error";
var error2 = "email error";


function doResult(result){

var n = result.split('|');
var err = n[0]; // this is the error id

document.getElementById(div_id).innerHTML = err;

return;
}

If I change the line;

document.getElementById(div_id).innerHTML = err;

to

document.getElementById(div_id).innerHTML = 'error1';

it shows the actual message as "name error" like it should????

Any help would be appreciated! Thanks!

Oziam
01-19-2011, 04:38 AM
Hey I finally worked it out, like I said it was pretty simple really,

replaced


document.getElementById(div_id).innerHTML = err;
with


document.getElementById(div_id).innerHTML = window[err];
// or also works

document.getElementById(div_id).innerHTML = this[err];
and now it works 100% :)

But how would I go about using the same principal for replacing the field name in the following line, the other method doesn't work???


document.forms[form_id].fieldname.focus();

devnull69
01-19-2011, 07:23 AM
document.forms[form_id].elements[fieldname].focus();

Oziam
01-19-2011, 07:58 AM
Thanks devnull69,

yeah I didn't know that one but if stumbled across this solution too;


document.forms[form_id][fieldname].focus();

cool to know more than one solution, you live and you learn!

Cheers!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum