views:

60

answers:

2

Hi!

I'm trying submit some form using ajax

//include jquery-1.4.2.min.js
var submitForm = document.createElement("FORM");
...
jQuery.post(submitForm.getAttribute('action'), submitForm.serialize(), function(data) {
      bla-bla
});

But there is error : "Error: submitForm.serialize is not a function" (FF)

What can I do? Thanks.

+3  A: 

submitForm is a DOM element, so you need to wrap it in a jQuery object to user jQuery methods like .serialize() on it, like this:

jQuery(submitForm).serialize()
Nick Craver
@Nick Craver: Thanks a lot.
Stas
+2  A: 

Create the form element with jQuery instead.

var submitForm = jQuery("<form />");

/* set attributes using attr */

// use attr instead of getAttribute
jQuery.post(submitForm.attr('action'), submitForm.serialize(), function(data) {
      bla-bla
});
BrunoLM
@BrunoLM: Thanks a lot.
Stas
Creating element with jQuery: http://api.jquery.com/jQuery/ --- jQuery attr: http://api.jquery.com/attr/
BrunoLM