27

When I submit a form using jQuery's serialize() method, everything gets submitted except the textarea in the form. Is this a common issue? I can't figure it out. The form works except for just the textarea which stays undefined???

<textarea form="new_note_form" id="note_text" name="note_text" required="required"></textarea>     
10
  • That doesn't happen to me. How do you know that the <textarea> is being left out?
    – Pointy
    Commented Jul 17, 2013 at 19:56
  • is the textarea in the element that you are serializing? Commented Jul 17, 2013 at 19:57
  • I alert all the values of the form when they are serialized, and textarea is undefined even if I have typed in some text. Do you mean I have to do something like $('#my_form textarea').serialize() ???
    – Amoeba
    Commented Jul 17, 2013 at 19:57
  • 1
    Note that serialize() will not add textarea value when the textarea includes the readonly attribute. I don't see that in your example, but just in case...
    – Will Lanni
    Commented Jul 25, 2013 at 18:23
  • 1
    Perhaps you're cloning the form (i.e. myForm.clone()) before sending it, which blanks out the textareas. It's a known - and annoying - bug: bugs.jquery.com/ticket/3016
    – kasimir
    Commented Nov 14, 2013 at 12:53

9 Answers 9

37

It does not work until you add name attribute to the textarea.

<textarea id="sLifeStyle3Content" name="sLifeStyle3Content" placeholder="HTML is allowed">
  <apex:outputText value="{!sLifeStyle3Content}" />
</textarea>
0
5

No it doesn't.

It works fine. http://jsfiddle.net/nuBkM/

<form>
    <input name="foo" value="bar"/><br>
    <textarea name="something">lorem ipsum</textarea>
</form>

The JavaScript

console.log($("form").serialize());
// => foo=bar&something=lorem+ipsum 

.serializeArray works too

console.log($("form").serializeArray());
// => [{name: "foo", value: "bar"}, {name: "something", value: "lorem ipsum"}] 
2
  • 2
    The reason OP's wasn't working was because they forgot the name attribute
    – TomSelleck
    Commented Jul 14, 2015 at 14:07
  • 2
    OP posted this <textarea form="new_note_form" id="note_text" name="note_text" required="required"></textarea>. Looks like it has a name attribute to me.
    – Mulan
    Commented Jul 14, 2015 at 19:49
1

Another work around for this is to turn the textarea value into a variable and pass that with the ajax call...

var comment = $('.note_comment').val();

           $.ajax({
               type: "POST",
               url: '/approot/rewrite.cfm/app.people/insertNote?format=json&Comment=' + comment,
               data: $("form[name='add_note_form']").serializeArray(),
               success: function(data)
               {
              alert('success');         
               }
             });
1

If the textarea is controlled by an editor like tinyMCE, you may need to call tinyMCE.triggerSave(), as described in this answer.

0

Works fine in the fiddle. http://jsfiddle.net/Ultimate/2Ey2A/ Testing with

$('button').click(function(){
    alert($('form').serialize());
});
0

I have the same experience. Submitting a form using $("#form_id").serialize() does not include the textarea fields. This behavior is consisent for the past 2 years in the only form that has textarea elements. Every now and then I re-examine form and code to conclude that it should work, but doesn't.

My work-around is, unsurprisingly, to first move the content of the textareas into hidden text boxes and then serialize the form data.

0

We ran into the same problem with a textarea not being serialized despite having the name attribute set, and noticed it depended on where in the form the textarea was placed. We had the luxury of moving the textarea to another location on the form to resolve the problem.

0

It leaves out the textarea , unless you Remove 'form="new_note_form"' from your textarea element.

I know it's against good practices, but, if you want to use jQuery's serialize function, you gotta remove this attribute from textarea element.

0

This is what I use to include/exclude each of the elements as I need them from the form. This method also makes our older forms serializable even though some of the elements only have id's defined and not names.

$( 'textarea' ).each( function() { 
  $(this).attr( 'type', 'textarea' ); 
});

$( 'input:text:not( ".excluded" ), input:checkbox, input:radio, textarea' ).each( function() {

  if (!$(this).hasClass( 'answer' )) { 
    $(this).addClass( 'answer' ); 
  }

  if ( !$(this).attr( "name" ) && $(this).attr( 'id' ) ) { 
    $(this).attr( "name", $(this).attr("id") ); 
  }

});

Then I call the function below to get my serialized array on the $( '.answer' ).change() event, on page navigation and on the $('form').submit() event. This method puts no noticeable load on the page performance that I can discern.

function storeFormData() {
  var serializedData = $( ".answer" ).serializeArray();
  var formDataObj = serializedData;
  var formDataString = JSON.stringify(formDataObj);
  localStorage.setItem(fso_id, formDataString);
  return formDataString;
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.