I am trying to present a list of possible smilys, when the user clicks on them the specific mark :P or :+ for example should be posted in the text area after or between the text he or she typed allready. I cant make it work. What am I doing wrong?
Textarea:
<textarea id="SendMessageForm" cols=1 rows=1 oninput='this.style.height = "";this.style.height = this.scrollHeight + "px"' type="text" wrap="virtual" data-token="<?php echo $token; ?>" data-messageto="<?php echo $LoadMessageFromUser; ?>" class="form-control hass-message-input" placeholder="Type your message"></textarea>
Sample for the smilys and the call to the function
<img src="emoticons/clown.gif" alt="Emoticon" onClick="insertEmoticon(':+');" />
<img src="emoticons/puh2.gif" alt="Emoticon" onClick="insertEmoticon(':P');" />
Javascript to make this happen
function insertEmoticon(smiley){
var myField = document.getElementById(SendMessageForm);
if (document.selection) {
myField.focus();
var sel = document.selection.createRange();
if (sel.text == "") {
sel.text += smiley;
myField.focus();
} else {
sel.text = smiley;
}
} else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
if (startPos == endPos) {
} else {
var str = myField.value.substring(startPos, endPos);
}
var myVal = smiley;
myField.value = myField.value.substring(0, startPos)+ myVal + myField.value.substring(endPos, myField.value.length);
} else {
myField.value += smiley;
}}
Any help is welcome (sorry for my language)
The issue with your code is that you haven't surrounded the value passed into getElementById with quotes.
To fix this, just use the code below.
document.getElementById("SendMessageForm");
The difference is that this code has SendMessageForm surrounded by quotes, unlike your code.
If you don't surround the value with quotes, then JavaScript will start looking for a variable called SendMessageForm, and use that value.
By surrounding it by quotes, you are saying that you want to search for SendMessageForm explicity.
In conclusion, you need to surround SendMessageForm with quotes to signify that it isn't a variable, but a string.