Last active
May 8, 2018 10:55
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<textarea name="description" id="description" rows="18" cols="55" wrap="physical"> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?