Results 1 to 2 of 2

Thread: selecting a new variable problem?

  1. #1
    Join Date
    Jan 2011
    Location
    QLD, Australia
    Posts
    23
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default selecting a new variable problem?

    Hi,
    I am new to javascript and cannot figure this out, it is probably very simple.

    I am trying to select 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!

  2. #2
    Join Date
    Jan 2011
    Location
    QLD, Australia
    Posts
    23
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default

    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();
    ok found it!

    Code:
    document.forms[form_id][fieldname].focus();
    // or
    document.forms[form_id].elements[fieldname].focus();
    Last edited by Oziam; 01-19-2011 at 08:04 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •