6

I want line breaks to be replaced with spaces when a paste operation is done on an input (text) element.

For example if the following text is pasted into google search:

foo
bar

it pastes as:

foo bar

By default, text input elements will stop at the first line break like so:

foo

I was able to find a solution with a textarea element using the following code:

$("#textarea_element").bind('paste', function(e) {
    var el = $(this);
    setTimeout(function() {
        $(el).val($(el).val().replace(/(\r\n|\n|\r)/gm," "));
    }, 100);
});

But I would like to have this functionality on an input element. Any ideas?

Sicco
  • 6,167
  • 5
  • 45
  • 61
nn8
  • 61
  • 1
  • 2
  • 1
    Browsers have this functionality built-in because you can't have line feeds in ``. Do you observe another behaviour? What browser do you use? – duri Aug 20 '12 at 21:40
  • I think that soulution is here by sanitizing Clipboard data http://stackoverflow.com/a/687115/206502 – Jirka Kopřiva Aug 20 '12 at 21:42
  • @duri when pasting line breaks in google they are replaced by spaces: only with firefox and chrome, this is not the case with explorer. – nn8 Aug 21 '12 at 16:32
  • 2
    this is actually browser behavior for all text input elements: firefox and chrome drop line feeds for spaces, and explorer stops at the first line feed. nothing to do with google in particular as i had initially thought. – nn8 Aug 21 '12 at 16:48

0 Answers0