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

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 01-19-2011, 03:52 AM   PM User | #1
Oziam
New to the CF scene

 
Join Date: Jan 2011
Location: Queensland Australia
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Oziam is an unknown quantity at this point
assigning a new variable problem?

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

Code:
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 is offline   Reply With Quote
Old 01-19-2011, 04:38 AM   PM User | #2
Oziam
New to the CF scene

 
Join Date: Jan 2011
Location: Queensland Australia
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Oziam is an unknown quantity at this point
Hey I finally worked it out, like I said it was pretty simple really,

replaced

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

Code:
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???

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

Last edited by Oziam; 01-19-2011 at 05:18 AM..
Oziam is offline   Reply With Quote
Old 01-19-2011, 07:23 AM   PM User | #3
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Code:
document.forms[form_id].elements[fieldname].focus();
devnull69 is offline   Reply With Quote
Old 01-19-2011, 07:58 AM   PM User | #4
Oziam
New to the CF scene

 
Join Date: Jan 2011
Location: Queensland Australia
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Oziam is an unknown quantity at this point
*** solved ***

Thanks devnull69,

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

Code:
document.forms[form_id][fieldname].focus();
cool to know more than one solution, you live and you learn!

Cheers!

Last edited by Oziam; 01-19-2011 at 08:08 AM..
Oziam 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 01:50 AM.


Advertisement
Log in to turn off these ads.