Skip to content

Instantly share code, notes, and snippets.

@webdevilopers
Last active May 8, 2018 10:55
Show Gist options
  • Save webdevilopers/fcb8c5fae8c644602e4a5f5e7921e697 to your computer and use it in GitHub Desktop.
Save webdevilopers/fcb8c5fae8c644602e4a5f5e7921e697 to your computer and use it in GitHub Desktop.
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?

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