How to write with jQuery such that when a user clicks on an image in textarea, the text is added to the end?

<textarea name="textarea" cols="60" rows="9" class="textfield" id="textcoment">Вот сюда чтобы добавлялся!</textarea> 

The text is inserted different! Pictures 11.

  • Pictures 1 or more with a handler? Is the text inserted always the same or depending on the pressed picture (if there are more than 1)? How does the text differ in the pictures? - iKuzko

3 answers 3

  $('img').click(function(){ var text = "text"; $('#textcoment').append(text); });​ 

    I understand we are talking about this?

      $('img').click(function(){ var text = $(this).attr('alt'); $('textarea').html(text); });​ 

    Example

    So?

     $('img').click(function(){ var text, text2, summa; text = $(this).attr('alt'); text2 = $('textarea').html(); summa = text2+' '+text; $('textarea').html(summa); }); 

    Example2

    Go

    Or so

     $('img').click(function(){ text = $(this).attr('alt'); $('#textcoment').append(' '+text); });​ 
    • Almost like that, but not exactly, I’m going to joss so that if there is already text being added to it - David
    • $ ('img'). click (function () {var text = $ (this) .attr ('alt'); $ ('textarea'). html ($ ('textarea'). html () + text) ;}); - Palmervan
     $('img').click(function(){ document.getElementById('textcoment').value += $(this).attr('alt'); });​ 

    Adds text from alt images to textcoment

    • Krasava Respect! - David
    • 2
      Why such a hodgepodge of pure js and jQuery? - Specter
    • Sorry, but what should prevent me from using javascript with javascript? In this case, .value + = "text" is a much more convenient form than .val (.val () + "text"). I use jquery where it is convenient (in this case, the selection of elements and hanging the event on them) and pure js where it is more convenient. $ (this) .attr ('alt') left in this form, since the text itself can be taken from different sources. Maybe $ (this) .attr ('alt'), or maybe $ (this) .data ('descritpion'). - iKuzko
    • from an aesthetic point of view, it looks like “not very” agree + = much more convenient, but after all $('#textcoment')[0] has not been canceled - Specter
    • $ ('# textcoment') [0] will do the same, only spend more time on it. I consider the construction of the form document.getElementById ('textcoment'). Value + = this.alt; better than $ ('# textcoment') [0] .value + = $ (this) .attr ('alt'); and therefore I advise you. I will repeat the construction ($ (this) .attr ('alt')) for example only, since the condition did not indicate which text exactly and where we are inserting it from. - iKuzko