0

Hi All I have 2 arrays on one page and some buttons which are performing onclick ajax calls - the line I am using to serialize the array is as seen below:

var data = $('form').serializeArray();

however the wrong form is being serialized - is there anyway to do serialize array by name or ID of a form field - I know this will be an extremely easy solution - I am completely new to JS

TotalNewbie
  • 1,004
  • 5
  • 13
  • 25

1 Answers1

2

Of course it's possible. If you have an ID set up on this form element (and you usually should), just specify it in the selector:

var someFormData = $('#some_form_id').serializeArray();
var anotherFormData = $('#another_form_id').serializeArray();

Note that using it like this is preferable to $('form#some_form_id') - being more specific when using ID selector is not a good idea.

It's almost the same with names - you just have to use so-called attribute selector:

var someFormData = $('[name=some_form]').serializeArray();
var anotherFormData = $('[name=another_form]').serializeArray();
Community
  • 1
  • 1
raina77ow
  • 103,633
  • 15
  • 192
  • 229
  • I never said it was impossible - in fact I mention the solution will be extremely easy I just didn't know how! Thankyou for the solution! – TotalNewbie Apr 05 '14 at 13:15