Skip to content

multiline placeholder #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
theraaz opened this issue Mar 26, 2016 · 4 comments
Open

multiline placeholder #14

theraaz opened this issue Mar 26, 2016 · 4 comments

Comments

@theraaz
Copy link

theraaz commented Mar 26, 2016

It would be good if multiline placeholder was supported for textarea with \n or something

@fidian
Copy link
Contributor

fidian commented Mar 28, 2016

I can see situations where this would be useful, such as showing an expected address format or something. However it breaks compatibility with HTML: https://www.w3.org/TR/html5/forms.html#the-placeholder-attribute

The attribute, if specified, must have a value that contains no "LF" (U+000A) or "CR" (U+000D) characters.

That said, it appears that browsers do allow you to use &#10 but there is no way to do it cross-browser very easily. Sometimes embedding actual newlines in the value might also work. More info from a StackOverflow post: http://stackoverflow.com/questions/7312623/insert-line-break-inside-placeholder-attribute-of-a-textarea

I am going to close this issue because it is going against the HTML spec even though the idea could be extremely useful. If anyone decides to hack in the code, I am open to receiving pull requests that make this functionality much easier and more cross-browser. The biggest hurdle is making it work for all browsers and especially when not shimmed (or we have to detect the values and decide on a field-basis whether or not we shim).

@fidian fidian closed this as completed Mar 28, 2016
@theraaz
Copy link
Author

theraaz commented Mar 29, 2016

I found a workaround that is working for me. https://gist.github.com/theraaz/5e900c084ebac454162e

@fidian
Copy link
Contributor

fidian commented Mar 29, 2016

Reopening this ticket. That solution is about what I do for browsers that do not support placeholders. I could incorporate their selector and still patch textareas even if the browser does support placeholders. There are still some difficult things:

  1. This has to be thoroughly tested cross-browser and I don't really have easy access to a wide enough range of browsers. Though this is really the least of my issues.
  2. Newline selection. The code you referenced uses $('textarea[placeholder*="\\n"]').each(function(){. I believe one should not use \n but instead use &#10. The HTML spec favors attributes with HTML encoded values, despite what I prefer. I'd like to stick with standards whenever possible.
  3. Browser feature detection of the placeholders that do and do not support newlines has now gotten far more tricky. This module should not be shimming a browser that fully supports newlines as expected.

@fidian fidian reopened this Mar 29, 2016
@plaa
Copy link

plaa commented Feb 9, 2018

The above workaround as an Angular directive (in CoffeeScript):

angular.module('app.directives').directive 'multilinePlaceholder', ($timeout) ->
  return {
    restrict: 'A',
    link: ($scope, element, attr) ->
      element.on 'focus', ->
        element.removeClass("placeholder")
        if element.val() == attr.multilinePlaceholder.replace(/\\n/g, "\n")
          element.val("")
      element.on 'blur', ->
        if element.val() == ""
          element.addClass("placeholder")
          element.val(attr.multilinePlaceholder.replace(/\\n/g, "\n"))
      
      $timeout -> 
        element.blur()
  }

Usage:

<textarea multiline-placeholder="one line\nsecond line\nthird line"></textarea>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants