Ayman
Ayman

Reputation: 11585

HTML perform paste into textarea

I have a textarea that I want to paste different text formats into. What would be ideal is to have several buttons each with a different kind of label.. One button to paste format A, another to paste format B... Each of these formats is just text. the JavaScript psuedocode for these would be:

function pasteFormatA() {
   var txt = clipboard.getTextContent();
   if(txt) {
       // sanitize text
       // then put in textarea
   }
}

Is that possible? Using Chrome, but cross browser solution would be better.

Upvotes: 0

Views: 1885

Answers (1)

mrtsherman
mrtsherman

Reputation: 39872

Reading the actual clipboard would be a violation of browser sandboxing. The best you can do is to sanitize the content after it is pasted.

This is a jQuery example of filtering based on active button pressed.

http://jsfiddle.net/xjTUY/1

<button class="active">A</button><button>B</button><button>C</button>
<br />
<br />
<textarea></textarea>

$('button').click(function() {
    $('button').removeClass('active');
    $(this).addClass('active');
});

$('textarea').bind('input propertychange', function() {
    var text = $(this).val();
    var activeFilter = $('button.active').html();
    switch (activeFilter) {
    case 'A':
        $(this).val(text.replace('a', ''));
        break;
    case 'B':
        $(this).val(text.replace('b', ''));
        break;
    case 'C':
        $(this).val(text.replace('c', ''));
        break;
    }
});

Upvotes: 1

Related Questions