2

I want to intercept a form submission, then pass the form as a variable to a Javascript function, then grab form element values within that specific form. In my example, there could be more than one form with class "formtype". I don't think what I have here is quite correct, but maybe close? How do I reference the specific form that has been submitted, in case of multiple "formtype" class forms?

$('.formtype').on('submit', function(e){
    var $submittedform=$(this);
    e.preventDefault();
    processForm($submittedform);
});

function processForm($submittedform){
    var email=$($submittedform+' .email').val();
}
1
  • make use of the data in the event you passing to function(e) on submit event. Commented Jun 27, 2018 at 18:45

1 Answer 1

2

You are pretty close, but use find() instead of the string concatenation

$('.formtype').on('submit', function(e) {
  var $submittedform = $(this);
  e.preventDefault();
  processForm($submittedform);
});

function processForm($submittedform) {
  console.log($submittedform.find('input.email').val())
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="formtype">
  <input class="email" value="foo">
  <input type="submit">
</form>

<form class="formtype">
  <input class="email" value="bar">
  <input type="submit">
</form>

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

1 Comment

Works great! Thanks.

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.