JHarley1
JHarley1

Reputation: 2112

Passing a parameter into a Javascript method

I have the following code:

function isFieldEmpty(input)
        {   
            if(document.frmRegister.input.value == "")
            {
                return false;
            }
            return true;
        }

I call it using isFieldEmpty("fieldName"). However, I think the "input" bit is incorrect...

Can anyone help?

Upvotes: 0

Views: 32

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074088

That code is looking for a property literally called "input" on frmRegister. To look for "fieldName" for example (the value of input), you want bracketed notation:

if(document.frmRegister[input].value == "")
// Change -------------^-----^

In JavaScript, you can access the property of an object using either dot notation and a literal property name (obj.foo) or using bracketed notation and a string property name (obj["foo"]). In the latter case, the property name string can be the result of any expression, including a variable or argument lookup.

Upvotes: 1

Related Questions