0

I must have read about arguments in functions and how they are variables and/or objects and pass value a million times and still real life examples keep throwing me off. The keyword "this" in this example points to the object of the function, being the form element. OK got that.

Now the function outside the window.onload event handler has a parameter named "frm". My question is whether it's necessary to give this parameter the same name as the id of the form element that will be passed to it (in this case 'frm')? I thought this didn't matter. For all it mattered one could put anything inside the argument just as long as it's reused as a local variable inside of the function itself.

window.onload = function() {
    // validation for submit button
    document.frmFlight.onsubmit = function() {
        //console.log("what is 'this'?: "+this);
        return validate(this);
    }       
}

//----------------------------------------------------------
function validate(frm) {
    var valid = true;
    return valid;
}   
1
  • I'm afraid the question about 'frm' isn't at all clear, can you explain a bit more? Commented Feb 8, 2011 at 15:34

5 Answers 5

1

It's not clear exactly what you're asking, but the name you give the argument in the function signature doesn't relate in any way to the name of anything you might be passing into the function. It can be (just about) anything you want it to be. These are all equivalent:

function validate(frm) {
    // ...
}

function validate(theForm) {
    // ...
}

function validate(foo) {
    // ...
}

Your use above is just fine, because when you set up an event handler in the way you have (by assigning a function to the onsubmit property), when the event fires, the function will be called such that this references the DOM element for the form. Assuming that's what the validate function uses its argument for (regardless of what it calls it), you're in good shape.

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

1 Comment

;yes this answers my question. It looked like it mattered but it looks like it doesn't. Thanks!
1

Function parameter names are arbitrary. The name you choose does not have any relationship to the type of thing later passed to the function.

My question is, is it needed to call the argument the form element's id in this case 'frm'?

It sounds like you're asking whether the name of the parameter in "validate" needs to match the ID of the DOM element that will be passed to it. (I would re-state this to use a word less overloaded than "call".)

The answer is no.

The general concept to understand is that your function has no awareness of the types of things that will be passed to it. Your function "validate" could take an argument of any type at run-time. The name of the parameter is irrelevant.

1 Comment

alright! yes that is my question exactly my course was causing me confusion. I basically needed confirmation on that it didn't matter.
0

frm refers to the variable passed to that function. In your example this object from the onLoad function. In the validate function it can be called anything you want.

Comments

0

Yeah, your question isn't too clear, but the parameter name (in this case frm) is just a reference to something you will pass in. Example:

function validate(frm){
  return true;
}

validate('Bob')
validate(3)

Comments

0

I don't know whether this is relevant to your question but given your reference to the 'this' keyword, if you were to do the following in your onsubmit handler:

return validate.call(this);

Then within your validate function, 'this' would refer to the form e.g.:

function validate(){
    var valid = this.elements[0].length > 0;
    return valid;
} 

Furthermore, in this situation, you could also do:

document.frmFlight.onsubmit = validate;

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.