Bug Tracker

Opened 9 years ago

Closed 8 years ago

Last modified 8 years ago

#9007 closed bug (wontfix)

Textarea value (via ".val()") differs from actual posted value (carriage returns are stripped)

Reported by: Pointy Owned by:
Priority: low Milestone: 1.next
Component: attributes Version: 1.5.2
Keywords: Cc: dmethvin, john, jaubourg
Blocked by: Blocking:

Description

See Ticket #6876 for some background.

When asked for the value of a textarea element, the ".val()" code strips all carriage return characters from the browser-reported value. However, when the value is submitted to the server, carriage returns are preserved (or added, by browsers that don't include them in the raw value), and (since #6876) jQuery makes sure they're there in the serialized version for XHR purposes.

The problem with this discrepancy between the field as reported by ".val()" and the actual submitted content is that when providing a sort of "maxlength" facility to report on available characters in a textarea, that code must account for the difference in cases where the carriage returns are preserved at the server (in my experience, a very likely thing). In other words, the "character countdown" must take into account the fact that when the textarea value (as reported by jQuery) contains newlines, then its actual length where it matters -- at the database, or in server-side validation code -- is greater by the number of newlines.

Now obviously this math is easy to do, and there may be all sorts of virtue in stripping those carriage returns. The behavior is not documented clearly, as far as I can tell; it's certainly absent from the description of ".val()". My observation is that in fact this discrepancy is unknown to many authors of exactly the sort of facility I described above.

http://jsfiddle.net/yNKmR/6/ is a crude jsFiddle that submits to a simple CGI script. The page provides a length counter, and the length (both raw and via ".val()") is sent to the server. It's quite clear to see that the posted textarea value contains more actual characters than accounted for by the jQuery ".val()" length. (Firefox and Webkit both behave like jQuery, so the raw length is also wrong.)

Change History (7)

comment:1 Changed 9 years ago by SlexAxton

Component: unfiledattributes
Keywords: val textarea added
Milestone: 1.next1.7
Priority: undecidedlow
Status: newopen

comment:2 Changed 9 years ago by dmethvin

This problem was precipitated by #6876 that converts a lone \n to \r\n for consistency with standards. Opera and IE have \r in the textarea value on Windows but most others don't. I think we want consistent cross-browser behavior here.

It seems like the only way to fix the problem of length mismatch is to have .val() consistently return \r\n but I shudder to think of what that would do to existing code.

comment:3 Changed 9 years ago by timmywil

So right now, textarea.val() is consistent across browsers, but not consistent with what is sent with ajax. It makes sense to me that carriage returns are required for ajax, but new lines are used when val is retrieved. I'd say this could be needsdocs and we could start with a plugin that does a valHook for textarea.

comment:4 Changed 8 years ago by addyosmani

Cc: dmethvin john jaubourg added

I'd like to +1 timmywil's suggestion that this be moved to a needsdocs and start life as a plugin. Thoughts?

comment:5 Changed 8 years ago by tomgrohl

I like timmywil's idea.

I've done a jsFiddle here with a valHook. Not sure if its much use or if I'm understanding the above correctly:

http://jsfiddle.net/tomgrohl/ekTwt/

If anything needs doing with this I'd love to help.

comment:6 Changed 8 years ago by dmethvin

Keywords: needsdocs added; val textarea removed
Milestone: 1.71.next
Resolution: wontfix
Status: openclosed

I agree, let's move this to a needsdocs. People who need to count exact characters can replace \n with \r\n either through a hook like tomgrohl showed or by explicit code.

It would be great to fix this somehow in jQuery but we're stuck between the W3C standard for transmitting content with \r\n and the near-universal convention of a single \n separating lines in a textarea.

comment:7 Changed 8 years ago by addyosmani

Keywords: needsdocs removed

Docs updated.

Note: See TracTickets for help on using tickets.