Note that there are some explanatory texts on larger screens.

plurals
  1. POSmilie system: Event to add image in textarea fails
    text
    copied!<p>I'm trying to create a personalized smilies system to trein my javascript. To accomplish that I have an offline system to save the various user smilies (I only save the urls).</p> <p>After obtaining the data I try to meke them appear above the textarea I want. So far so good!</p> <p>Now, the problem comes when it's time to add the events to the images. I try to add event listeners to each image but no matter which image I press only the last image event is triggered.</p> <p>This is: all images appear side by side correctly but what is inserted in the textarea is the last image that is iterated in the cycle.</p> <p>Meaningful code:</p> <pre><code>/* Insert the code in the right place in the textarea*/ function putInTxtarea(text, textarea) { // Mozilla text range replace. if (typeof(textarea.selectionStart) != "undefined") { var begin = textarea.value.substr(0, textarea.selectionStart); var end = textarea.value.substr(textarea.selectionEnd); var scrollPos = textarea.scrollTop; textarea.value = begin + text + end; if (textarea.setSelectionRange) { textarea.focus(); textarea.setSelectionRange(begin.length + text.length, begin.length + text.length); } textarea.scrollTop = scrollPos; } // Just put it on the end. else { textarea.value += text; textarea.focus(textarea.value.length - 1); } var elem = document.createElement("div"); elem.id = "mySmilies"; elem.innerHTML = ""; for each (url in smiliesUrl){ var img = document.createElement("img"); img.src = url; img.style.cursor = "pointer"; img.addEventListener('click', function(){putInTxtarea('[img]'+url+'[/img]', document.getElementsByName('message')[0]); };, false); // here is the event attaching elem.appendChild(img); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload