0

I have been reading through a few pages in order to figure out how to insert the content from a html tag into a textarea element.

I have found a page with an example here on stackoverflow which I tried to modify, however since I'm not experienced in using jquery I (of course) quickly ran into trouble ! because I simply don't know how to "connect" a click-event on a html tag with an event which inserts the content from a html tag for example:

<p>something</p>

into a textarea element.

Here is what I have so far:

$("p").click(function() {
 $(this).
  $("textarea").insertAtCaret('text');

and the main issue is what write after:

$(this).????

I have made a jsfiddle where my project is shown

I hope that someone would be so kind as to help me out or at least point me in the right direction - thank you in advance :o)

NeoFox1972
  • 25
  • 1
  • 7

2 Answers2

0

to get a tag content use text() and to get an input/textarea content use val() also to edit their content use text('some content ') / val('some content ')

$("p").click(function() {
var content=$(this).text();
$("textarea").val(content);
Aref Ben Lazrek
  • 1,056
  • 1
  • 11
  • 19
0

I made a jsfiddle for you which get first name and last name from 2 div (you can change them to p, no matter), and when you click on the button, the textarea's text will replace with new names. hope it works, please confirm if it solves your problem.

Html:

    <h2>
we have 2 paragraph and we want to add them ro our textare.
</h2>

<div class="FName">Arya</div>
<div class="LName">Ag</div>
<form action="#">
  <textarea name="message" rows="10" cols="30">
Insert [firstname] or [lastname] at the position of the cursor in this textarea while keeping the content already there 
</textarea>
<br/>
<br/>
<input type="button" value="update" id="updater"/>

Js:

$('#updater').on('click',function(){
var FName= $('.FName').text();
var LName= $('.LName').text();

$('textarea').text($('textarea').val().replace("[firstname]",FName).replace('[lastname]',LName));

})
Arya Aghaei
  • 495
  • 9
  • 22
  • No, this don't solve my problem as I'm not interested in replacing a piece of text with another - all I want is to insert a piece of text for example [firstname] at the cursor's position - thank you for your effort :o) – NeoFox1972 Dec 17 '17 at 15:30