Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Prevent extra space from line break when adding text at then end of a too small textarea
function placeholder2text(textElementId, text)
{
var textElement = document.getElementById(textElementId);
if(textElement.createTextRange && textElement.caretPos)
{
var caretPos = textElement.caretPos;
caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
textElement.focus();
}
else
{
textElement.value += text;
textElement.focus();
}
}
<textarea name="description" id="description" rows="18" cols="55" wrap="physical">
@webdevilopers
Copy link
Author

webdevilopers commented May 8, 2018

This script adds a text e.g. in the format {CATEGORY->placeholder} at the end of an textarea. Unfortunately if the textarea is too small and the word is too long it adds an extra space where the line breaks. How to prevent this?

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment