Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

222
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda