0

Here on page load i am trying to store the values from form to variables. Later i would like to use those variables (if and only if i have data in that) to other purpose.

This is how i approached ( on page load )

render : function() {
 frm = document.Form;

 if (frm.Name1.value){
                value1 = frm.Name1.value;
        }
 if (frm.Name2.value){
            value2 = frm.Name2.value;
        }
if (frm.Name3.value){
            value3 = frm.Name3.value;
        } 

}

Based on certain condition / selection i will call this function to assign initially loaded values

function add_values ( ) {
      if (value1.length){
            frm.Name1.value = value1;
        }
       if (value2.length){
            frm.Name2.value = value2;
        }
        if (value3.length){
            frm.Name3.value = value3;
        }
}

Error: ( in firebug )

value3 is not defined
[Break On This Error] if (value3.length){ 

What did i try ?

if (undefined != value3)
1

4 Answers 4

2

try

if(typeof value3 != "undefined")

more on typeof

Sign up to request clarification or add additional context in comments.

1 Comment

typeof is exactly what it sounds like. it returns the type of the variable in string format. see here: developer.mozilla.org/en/JavaScript/Reference/Operators/typeof so in this case if typeof returns "undefined" then you know it was not previously set.
1

You can use:

typeof value3 != 'undefined'

Comments

0

To check if something is undefined:

if (typeof value3 === 'undefined') {
    // It's undefined!
}

Comments

0

Just check whether the variable's type is undefined:

if (typeof value3 !== 'undefined') {
  // value3 is defined
}

If you want, you can make a PHP-like function for that:

function isset(variable) {
  return typeof variable !== 'undefined';
}

if (isset(value3)) {
  // value3 is defined
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.