1

There is a JavaScript function that is normally called through a form tag, using onsubmit.

In the code it looks like this:

<form method="post" name="form1" id="form1" onsubmit="return validate(this)">

If I were going to call the validate function through some other method, what variable would I pass it? I mean, the 'this' in the onsubmit call, what would I put there instead? Would it be the id of the form, so "form1"?

1
  • 2
    What do you mean by "some other method"? Also, can you give us more info on the validate function? Commented May 6, 2012 at 1:20

2 Answers 2

2

this refers to the element that the tag creates, that is, the form element. So you would call it with:

validate(document.getElementById('form1');

For more information on events, check out this introduction. The onsubmit attribute you're using is an inline event handler.

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

Comments

0

this in that context is the DOM element(the form)

So you should use it like this:

var form = document.getElementById('form1');
validate(form);

4 Comments

if not through a submit button, you can allways use form1.submit(); or to be more correct document.getElementById('form1').submit();
@Frank. What are you talking about?
I'm saying maybe he can just trigger it whenever he wants to call it.
@Frank. I don't know what he wants, maybe you should comment to him. Place your comment on the question.

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.