3

I have the following form:

<form id="searchform_txtRepeatComplaint" name="searchform_txtRepeatComplaint">
   <input name="datasource" type="hidden" value="TDA">
   <input name="from" type="hidden" value="eci_data">
   <input name="where" type="hidden" value="">
   <input name="componentname" type="hidden" value="txtRepeatComplaint">
   <input name="staticstrings" type="hidden" value="">
   <input name="staticfields" type="hidden" value="">
   <input class="nostyle" name="returnfield" type="hidden" value="Complaintinformation_ID">
   <input class="nostyle" name="returnid" type="hidden" value="Complaintinformation_ID">
eCI ID:<input  class="nostyle" type="text" name="Complaintinformation_ID" id="Complaintinformation_ID" style="width:150px;"/> 
Complaint number: <input  class="nostyle" type="text" name="eci_number" id="eci_number" style="width:150px;"/>
</form>

This is a form within a bigger form tag. I also have the following JS code:

var data_save = $("#searchform_txtRepeatComplaint").serializeArray();
alert(data_save);

The alert shows up empty. (not even [Object] is shown, it's just an empty alert box). What am I doing wrong?

SOLUTION for future readers: I found how to get around this error. I changed the FORM into a DIV and added the input selector, like such:

var data_save = $("#searchform_txtRepeatComplaint input").serializeArray();

ALSO: the docs state serializeArray() will not work with a form in a form (which is illegal HTML anyway)

1 Answer 1

2

You missed a # in the selector

var data_save = $("#searchform_txtRepeatComplaint").serializeArray();

instead of

var data_save = $("searchform_txtRepeatComplaint").serializeArray();

Also, to debug code, you should use console.log() instead of alert(). More useful to check objects/array.

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

4 Comments

it's not working when I add the selector. Could it be because it's a form tag in a form tag?
I got it to work in the meantime. When I change the FORM to a DIV, it works :) I found out that serializeArray doesn't work with a form in a form (it's somewhere in the docs)
That's weird.. "This method can act on a jQuery object that has selected individual form elements, such as <input>, <textarea>, and <select>. However, it is typically easier to select the <form> tag itself for serialization:".. api.jquery.com/serializeArray. There's probably an ID conflict in your DOM.
from the jquery docs at docs.jquery.com/Ajax/serializeArray: 'Note that serializeArray() only works on form elements, using this method on another element will not work. Also, this method will not give any results on nested forms, which are illegal in HTML anyway':) my first step was to cehck for duplicate ID's in the DOM. I'm telling you, it's the form in form thing:)

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.