Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

220
Views
Javascript insert text / smily in textarea by id

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)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!