There is a sprig of comments. You must pass the user name, which is enclosed in <p> tags in a textarea with a dynamic id. Tell me how it can be implemented and how can it look like an example?
- If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky ♦
1 answer
Well, I think the answer depends on what you have. For example, if textarea has a name attribute, you can find it using the getElementsByName function. It will return all items with the given name. In our example, it is unique, so we select the first element, change its value and that's it.
On <p> hang onclick with the insertNick function. In the parameter we pass the text of the tag. this is the tag itself. textContent returns the text contained within the tag. If there are other tags in <p> , it will return all the text contained in them, subtracting the tags.
<script> function insertNick(nick) { var container = document.getElementsByName("entity")[0]; container.value += nick; } </script> <p onclick="insertNick(this.textContent);"> UserName </p> <form> <textarea name="entity"></textarea> </form> Or you can find the form in which the textarea is located, get a textarea if you know its location. There is a form, the necessary element in it is the second. So the desired item with index one.
<script> function insertNick(nick) { var form = document.getElementById("entity"); form.elements[1].value += nick; } </script> <p onclick="insertNick(this.textContent);"> UserName </p> <form id="entity"> <input type="text" value="Дата"> <br> <textarea> Ваш ник: </textarea> </form> Or you can get a form, and find the desired textarea by the name attribute.
// получили форму. Получаем элемет формы с именем entity var elem = form.elements.entity; PS In JS I'm not an expert, but I would solve this problem somehow.