Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

224
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda