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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Check for the existence of a variable with a calculated name?

    Hi, I'm processing a form with numbered fields, but I don't know how many fields there are. Might be 1, might be 20. So I'm iterating through 1-20 and trying the following test:

    for(i=1; i<=20; i++) {
    if(typeof(eval('document.edit_orderitem_form.field_'+i+'.value')) != 'undefined') {
    [doing stuff with the form field here]
    }
    }

    The script quits on that line with "Undefined value" showing up in Safari's error console. I just can't figure out how to eval() the name of a field when it may or may not exist, and have the script continue gracefully. Thanks.

  • #2
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    No need for eval() here. Try to replace this part:
    Code:
    for(i=1; i<=20; i++) {
    if(typeof(eval('document.edit_orderitem_form.field_'+i+'.value')) != 'undefined') {

    ...with:
    Code:
    for(var i=1; i<=20; i++) {
    if(typeof document.edit_orderitem_form.elements['field_'+i] != 'undefined'){
    P.S., I haven't tested it at my end, but hopefully, should work.

    Hope that helps.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • Users who have thanked rangana for this post:

    briandunning (01-29-2009)

  • #3
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Interesting. I will try it, but there is one more complication: For clarity, I left one more variable out of my example code. There are multiple forms on the page, so there is also a variable ii that tells what form to use. Is it possible to use the elements[] twice, once for the form name, and once for the field name?

  • #4
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    If you want to loop through all of the forms, you can use form (forms[]) collection instead of the elements[].

    For further reading:
    http://www.w3schools.com/htmldom/coll_doc_forms.asp

    Hope that helps.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #5
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I'm not looping through forms, I'm only receiving input from one of them. So what I need to get is this:

    Code:
    document.form_XXX.field_YYY.value
    where XXX is a value passed to my script, and YYY is determined by looping and iterating. The first few YYYs will exist, but not all of them.

    Can I say something like this?:

    Code:
    formname = 'form_' + XXX;
    for(i=1; i<=20; i++) {
      if(typeof document.form[formname].elements['field_'+i] != 'undefined'){
        [doing stuff with the form field here]
      }
    }
    Whew... :-)

  • #6
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I got it figured out. The solution was to not use eval() at all, and use try{} instead of checking for undefined. Thanks.
    Code:
    for(i=1; i<=20; i++) {
      try {
        [do stuff with:]  document.forms['form_'+xxx].elements['field_'+i].value;
      }
      catch(err) {
        // Do nothing
      }
    }


  •  

    Tags for this Thread

    Posting Permissions

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