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

1
  • You can use $('form#formid') to get the right form. Commented Apr 5, 2014 at 13:15

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

1 Comment

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!

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.