Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jan 2011
    Location
    Queensland Australia
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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!

  • #2
    New to the CF scene
    Join Date
    Jan 2011
    Location
    Queensland Australia
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #3
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    Code:
    document.forms[form_id].elements[fieldname].focus();

  • #4
    New to the CF scene
    Join Date
    Jan 2011
    Location
    Queensland Australia
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

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


  •  

    Posting Permissions

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