how to replace line feeds / carriage returns when pasting into an input element (like google)

Viewed 3023

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?

0 Answers
Related