Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6327301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:15:09+00:00 2026-05-24T17:15:09+00:00

So I have this snippet which captures the submit: $(function(){ $(‘#xhr2_form’).submit(function(e){ e.preventDefault(); sendForm(); });

  • 0

So I have this snippet which captures the submit:

$(function(){
    $('#xhr2_form').submit(function(e){
        e.preventDefault();
        sendForm();
    });
});    

and this snippet to apply the XHR 2 form submission

var xhr = new XMLHttpRequest();

function sendForm() {
    var myForm = $('#xhr2_form');

    // get fields
    var values = {};
    $.each($('#xhr2_form').serializeArray(), function(i, field){
        values[field.name] = $(this).val();
    });

    // append data
    var formData = new FormData(myForm);
    formData.append('type',values['type']);

    xhr.open('POST', '/test/xhr2', true);
    xhr.onload = function(e){};

    xhr.send(formData);
}

the problem is the api for FormData accepts an HtmlFormElement as documented here:

http://dev.w3.org/2006/webapi/XMLHttpRequest-2/#the-formdata-interface

the problem is I’m trying to retrieve that HTMLFormElement from the submit event of Jquery, but I don’t know how.

without JS, this is how it’s done:

<form id="myform" name="myform" action="/server">
  <input type="text" name="username" value="johndoe">
  <input type="number" name="id" value="123456">
  <input type="submit" onclick="return sendForm(this.form);">
</form>

function sendForm(form) {
  var formData = new FormData(form);

  formData.append('secret_token', '1234567890'); // Append extra data before send.

  var xhr = new XMLHttpRequest();
  xhr.open('POST', form.action, true);
  xhr.onload = function(e) { ... };

  xhr.send(formData);

  return false; // Prevent page from submitting.
}

so how do I get the HTMLFormElement in Jquery?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T17:15:11+00:00Added an answer on May 24, 2026 at 5:15 pm

    Is the issue just that you’re providing a jQuery object instead of a DOM node? If that’s the case, you should be fine just retrieving the first element from the object, which (unless you have something invalid like another xhr2_form on the page) will give you the form element you’re looking for.

    function sendForm() {
      var myForm = $('#xhr2_form'); // this is a jQuery object
    
      // ...
    
      /*
       *  myForm is a jQuery object
       *  myForm[0] is the first element of the object, in this case whichever
       *    node has the ID "xhr2_form"
       */
      var formData = new FormData(myForm[0]);
    
      // ...
    }
    

    You could also retrieve it using .get (not to be confused with $.get) like so:

    $("#xhr2_form").get(0);  // or myForm.get(0);
    

    Also, as a side note, when you use jQuery to search for an ID, like $("#some_id"), after it’s determined that some_id is a potentially valid ID, it uses getElementById to find the node anyway.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code snippet inside a function that checks if an object exists
I have encounters this abap snippet that I have difficulty to comprehend. call function
I have this snippet of code which I am considering to simplfy: if (numberOfResults
I have this certain method(snippet below) for which I want to get the XML
I have a code snippet which connects to a MySQL database like this (not
I have this snippet of code, which fails at var link: var obj =
I have this snippet of code in which I want to handle the downlaod
I have this cool little snippet that I found, which adds a shadow to
I have a piece of code which looks like this: Snippet A: class Creature
I have this PHP contact form which disallows a submission from an existing IP.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.