Seb
Seb

Reputation: 61

Can copy and paste text be recognised in textarea

When I copy and paste some text in the textarea below e.g. hello world and add a bullet, the bullet gets added at the end of the sentence e.g. hello world•

So how can I make sure that the bullets appear at the beginning of the sentences e.g. • hello world? It looks like the textarea doesn't recognised copied text for some reasons.

$('.add-bullet').click(function() {
    $(this).parent().next('textarea').val(function(idx, value){
        return value + '\u2022';
    });
    return false;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

 <div><a href="#" class="add-bullet">Add bullet</a></div>
    <textarea type="text" name="tata" placeholder="Write something.." ></textarea>

Upvotes: 0

Views: 199

Answers (1)

Michael Rodriguez
Michael Rodriguez

Reputation: 2176

The problem is that you have return value + '\u2022'; which means take the current value, and add to it the unicode character which represents a bullet. It should be the other way around. This code can be improved to only modify the current line of text, in case the textarea has multiple lines.

$('.add-bullet').click(function() {
    $(this).parent().next('textarea').val(function(idx, value){
        return '\u2022 ' + value;
    });
    return false;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

 <div><a href="#" class="add-bullet">Add bullet</a></div>
    <textarea type="text" name="tata" placeholder="Write something.." ></textarea>

Upvotes: 1

Related Questions