Appending text into single text area - separating by paragraph (using javascript)










0















I found this answer already that helped, but does not answer this question: Append text to textarea with javascript



To borrow from that as an example:
Clicking all three of the list items below, would read "HelloWorldEarthlings"



<textarea id="alltext"></textarea>
<ol onclick="addText(event)">
<li>Hello</li>
<li>World</li>
<li>Earthlings</li>
</ol>
<script>
function addText(event)
</script>


I'd like to add a list and then distinguish clicks from each list into their own paragraph,



For example, I'd like to click the first option on each list below:



<textarea id="alltext"></textarea>
<ol onclick="addText(event)">
<li>Hello</li>
<li>Greetings</li>
<li>Salutations</li>
</ol>

<ol onclick="addText(event)">
<li>Humans!</li>
<li>Peasants?</li>
<li>Earthlings.</li>
</ol>
<script>
function addText(event)
</script>


To somehow print:



Hello



Humans!



edit: I'd like to put upwards of ten lists - not just two.










share|improve this question
























  • By paragraph do you mean Text paragraph, or separated by a paragraph tag? Right now alltext is a textarea, so you would have to change that to another container if you want to use html tags on the content within it.

    – zfrisch
    Nov 13 '18 at 21:17












  • document.getElementById("alltext").value += String.fromCharCode(10) + ( targ.textContent || targ.innerText); You might want to have two line breaks in which case you would just use String.fromCharCode(10) twice.

    – Adam H
    Nov 13 '18 at 21:18












  • What other container could I change it to, and how?

    – Ryan'sDad
    Nov 13 '18 at 21:19











  • If it doesn't need to be a textarea you could append p tags to a div and get actual paragraphs.

    – Adam H
    Nov 13 '18 at 21:19











  • Adam H - I'd like to have just a blank document with the actual paragraphs, I don't need (or want) any visible html or anything. How would I do what you suggest?

    – Ryan'sDad
    Nov 13 '18 at 21:23















0















I found this answer already that helped, but does not answer this question: Append text to textarea with javascript



To borrow from that as an example:
Clicking all three of the list items below, would read "HelloWorldEarthlings"



<textarea id="alltext"></textarea>
<ol onclick="addText(event)">
<li>Hello</li>
<li>World</li>
<li>Earthlings</li>
</ol>
<script>
function addText(event)
</script>


I'd like to add a list and then distinguish clicks from each list into their own paragraph,



For example, I'd like to click the first option on each list below:



<textarea id="alltext"></textarea>
<ol onclick="addText(event)">
<li>Hello</li>
<li>Greetings</li>
<li>Salutations</li>
</ol>

<ol onclick="addText(event)">
<li>Humans!</li>
<li>Peasants?</li>
<li>Earthlings.</li>
</ol>
<script>
function addText(event)
</script>


To somehow print:



Hello



Humans!



edit: I'd like to put upwards of ten lists - not just two.










share|improve this question
























  • By paragraph do you mean Text paragraph, or separated by a paragraph tag? Right now alltext is a textarea, so you would have to change that to another container if you want to use html tags on the content within it.

    – zfrisch
    Nov 13 '18 at 21:17












  • document.getElementById("alltext").value += String.fromCharCode(10) + ( targ.textContent || targ.innerText); You might want to have two line breaks in which case you would just use String.fromCharCode(10) twice.

    – Adam H
    Nov 13 '18 at 21:18












  • What other container could I change it to, and how?

    – Ryan'sDad
    Nov 13 '18 at 21:19











  • If it doesn't need to be a textarea you could append p tags to a div and get actual paragraphs.

    – Adam H
    Nov 13 '18 at 21:19











  • Adam H - I'd like to have just a blank document with the actual paragraphs, I don't need (or want) any visible html or anything. How would I do what you suggest?

    – Ryan'sDad
    Nov 13 '18 at 21:23













0












0








0








I found this answer already that helped, but does not answer this question: Append text to textarea with javascript



To borrow from that as an example:
Clicking all three of the list items below, would read "HelloWorldEarthlings"



<textarea id="alltext"></textarea>
<ol onclick="addText(event)">
<li>Hello</li>
<li>World</li>
<li>Earthlings</li>
</ol>
<script>
function addText(event)
</script>


I'd like to add a list and then distinguish clicks from each list into their own paragraph,



For example, I'd like to click the first option on each list below:



<textarea id="alltext"></textarea>
<ol onclick="addText(event)">
<li>Hello</li>
<li>Greetings</li>
<li>Salutations</li>
</ol>

<ol onclick="addText(event)">
<li>Humans!</li>
<li>Peasants?</li>
<li>Earthlings.</li>
</ol>
<script>
function addText(event)
</script>


To somehow print:



Hello



Humans!



edit: I'd like to put upwards of ten lists - not just two.










share|improve this question
















I found this answer already that helped, but does not answer this question: Append text to textarea with javascript



To borrow from that as an example:
Clicking all three of the list items below, would read "HelloWorldEarthlings"



<textarea id="alltext"></textarea>
<ol onclick="addText(event)">
<li>Hello</li>
<li>World</li>
<li>Earthlings</li>
</ol>
<script>
function addText(event)
</script>


I'd like to add a list and then distinguish clicks from each list into their own paragraph,



For example, I'd like to click the first option on each list below:



<textarea id="alltext"></textarea>
<ol onclick="addText(event)">
<li>Hello</li>
<li>Greetings</li>
<li>Salutations</li>
</ol>

<ol onclick="addText(event)">
<li>Humans!</li>
<li>Peasants?</li>
<li>Earthlings.</li>
</ol>
<script>
function addText(event)
</script>


To somehow print:



Hello



Humans!



edit: I'd like to put upwards of ten lists - not just two.







javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 21:21







Ryan'sDad

















asked Nov 13 '18 at 21:12









Ryan'sDadRyan'sDad

1615




1615












  • By paragraph do you mean Text paragraph, or separated by a paragraph tag? Right now alltext is a textarea, so you would have to change that to another container if you want to use html tags on the content within it.

    – zfrisch
    Nov 13 '18 at 21:17












  • document.getElementById("alltext").value += String.fromCharCode(10) + ( targ.textContent || targ.innerText); You might want to have two line breaks in which case you would just use String.fromCharCode(10) twice.

    – Adam H
    Nov 13 '18 at 21:18












  • What other container could I change it to, and how?

    – Ryan'sDad
    Nov 13 '18 at 21:19











  • If it doesn't need to be a textarea you could append p tags to a div and get actual paragraphs.

    – Adam H
    Nov 13 '18 at 21:19











  • Adam H - I'd like to have just a blank document with the actual paragraphs, I don't need (or want) any visible html or anything. How would I do what you suggest?

    – Ryan'sDad
    Nov 13 '18 at 21:23

















  • By paragraph do you mean Text paragraph, or separated by a paragraph tag? Right now alltext is a textarea, so you would have to change that to another container if you want to use html tags on the content within it.

    – zfrisch
    Nov 13 '18 at 21:17












  • document.getElementById("alltext").value += String.fromCharCode(10) + ( targ.textContent || targ.innerText); You might want to have two line breaks in which case you would just use String.fromCharCode(10) twice.

    – Adam H
    Nov 13 '18 at 21:18












  • What other container could I change it to, and how?

    – Ryan'sDad
    Nov 13 '18 at 21:19











  • If it doesn't need to be a textarea you could append p tags to a div and get actual paragraphs.

    – Adam H
    Nov 13 '18 at 21:19











  • Adam H - I'd like to have just a blank document with the actual paragraphs, I don't need (or want) any visible html or anything. How would I do what you suggest?

    – Ryan'sDad
    Nov 13 '18 at 21:23
















By paragraph do you mean Text paragraph, or separated by a paragraph tag? Right now alltext is a textarea, so you would have to change that to another container if you want to use html tags on the content within it.

– zfrisch
Nov 13 '18 at 21:17






By paragraph do you mean Text paragraph, or separated by a paragraph tag? Right now alltext is a textarea, so you would have to change that to another container if you want to use html tags on the content within it.

– zfrisch
Nov 13 '18 at 21:17














document.getElementById("alltext").value += String.fromCharCode(10) + ( targ.textContent || targ.innerText); You might want to have two line breaks in which case you would just use String.fromCharCode(10) twice.

– Adam H
Nov 13 '18 at 21:18






document.getElementById("alltext").value += String.fromCharCode(10) + ( targ.textContent || targ.innerText); You might want to have two line breaks in which case you would just use String.fromCharCode(10) twice.

– Adam H
Nov 13 '18 at 21:18














What other container could I change it to, and how?

– Ryan'sDad
Nov 13 '18 at 21:19





What other container could I change it to, and how?

– Ryan'sDad
Nov 13 '18 at 21:19













If it doesn't need to be a textarea you could append p tags to a div and get actual paragraphs.

– Adam H
Nov 13 '18 at 21:19





If it doesn't need to be a textarea you could append p tags to a div and get actual paragraphs.

– Adam H
Nov 13 '18 at 21:19













Adam H - I'd like to have just a blank document with the actual paragraphs, I don't need (or want) any visible html or anything. How would I do what you suggest?

– Ryan'sDad
Nov 13 '18 at 21:23





Adam H - I'd like to have just a blank document with the actual paragraphs, I don't need (or want) any visible html or anything. How would I do what you suggest?

– Ryan'sDad
Nov 13 '18 at 21:23












3 Answers
3






active

oldest

votes


















1














It seems I've taken a slightly different approach than others used here. I've elected to do everything at run-time, so you don't have to pre-calc anything. Just throw your data into the page and it should 'just work' tm.



I saw you wanted o have an arbitrary number of lists, so allowed for that. Also, presumably, you'd like an arbitrary number of options in each list. What is uncertain however, is what the expected long-term behaviour of such a control would be. I.e if I have two lists, can I only evr expect to see two words in the box, or will it just continue to append the most recently chosen one ad-infinitum?



So, when the page loads I quickly examine the page's contents, looking for and counting instances of <OL> elements. I then create an array with this many elements before setting an attribute of each list to tell the list what it's index is.



When an <li> is clicked, the word is retrieved, the index of the list in which it resides is retrieved and then the word is stuffed into the array we created earlier. We then ask for a re-draw, in which case all of the elements of the array are stuck together with a space(' ') in between each element, before it's blasted to the screen.






"use strict";
function byId(id)return document.getElementById(id)
function newEl(tag)return document.createElement(tag)
window.addEventListener('load', onWindowLoaded, false);

var wordArray;

function onWindowLoaded(evt)

var orderedLists = document.querySelectorAll('ol');
var listArray = [...orderedLists];
wordArray = new Array(listArray.length);

listArray.forEach( function(curList, index)

curList.dataset.listIndex = index;
curList.addEventListener('click', onListClicked, false);

);


function onListClicked(evt)

var text = evt.target.textContent;
var arrayIndex = parseInt(this.dataset.listIndex);
wordArray[arrayIndex] = text;
renderText();


function renderText()

var str = wordArray.join(" ");
byId('output').textContent = str;

	<textarea id='output'></textarea>
<ol>
<li>Hello</li>
<li>Greetings</li>
<li>Salutations</li>
</ol>
<ol>
<li>Humans</li>
<li>Peasants</li>
<li>Earthlings</li>
</ol>








share|improve this answer























  • I really like your approach, however; I think OP wanted a running list of the actions taken, should be really easy to update your solution to handle that.

    – Adam H
    Nov 13 '18 at 22:46











  • Actually - I like that this seems to handle only one response from each list. How would you do what @Adam H did with inserting 2 line breaks after each one?

    – Ryan'sDad
    Nov 14 '18 at 13:44











  • @Ryan'sDad - it's actually surprisingly easy. Just change wordArray.join(" "); into wordArray.join("nn"); This way, we join each of the array members together with a pair of new-lines, instead of a single space.

    – enhzflep
    Nov 17 '18 at 0:36


















0














If you want text paragraphs:



document.getElementById("alltext").value += "t" + (targ.textContent || targ.innerText) + "n";





<textarea id="alltext"></textarea>
<ol onclick="addText(event)">
<li>Hello</li>
<li>Greetings</li>
<li>Salutations</li>
</ol>

<ol onclick="addText(event)">
<li>Humans!</li>
<li>Peasants?</li>
<li>Earthlings.</li>
</ol>
<script>
function addText(event) event.srcElement;
document.getElementById("alltext").value += "t" + (targ.textContent
</script>





If you want HTML Paragraphs you can change the alltext textarea to a div, and use the following code:



document.getElementById("alltext").innerHTML += "<p>" + (targ.textContent || targ.innerText) + "</p>";





<div id="alltext"></div>
<ol onclick="addText(event)">
<li>Hello</li>
<li>Greetings</li>
<li>Salutations</li>
</ol>

<ol onclick="addText(event)">
<li>Humans!</li>
<li>Peasants?</li>
<li>Earthlings.</li>
</ol>
<script>
function addText(event)
var targ = event.target
</script>








share|improve this answer






























    0














    Here are a couple of possible solutions, you can pick which one you like best.



    Append all to a single textarea with line breaks:






    function addText(event) 
    var targ = event.target

    <textarea id="alltext"></textarea>
    <ol onclick="addText(event)">
    <li>Hello</li>
    <li>Greetings</li>
    <li>Salutations</li>
    </ol>

    <ol onclick="addText(event)">
    <li>Humans!</li>
    <li>Peasants?</li>
    <li>Earthlings.</li>
    </ol>





    Append P tags to a Div:






    var container = document.getElementById('alltext');

    function addText(event)

    <p id="alltext"></p>
    <ol onclick="addText(event)">
    <li>Hello</li>
    <li>Greetings</li>
    <li>Salutations</li>
    </ol>

    <ol onclick="addText(event)">
    <li>Humans!</li>
    <li>Peasants?</li>
    <li>Earthlings.</li>
    </ol>








    share|improve this answer

























    • I really like the first option - but is it possible to add a second line break so that it looks more like the second example? Thank you so much!

      – Ryan'sDad
      Nov 13 '18 at 21:36











    • @Ryan'sDad I updated the first solution for you.

      – Adam H
      Nov 13 '18 at 22:18










    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289558%2fappending-text-into-single-text-area-separating-by-paragraph-using-javascript%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    It seems I've taken a slightly different approach than others used here. I've elected to do everything at run-time, so you don't have to pre-calc anything. Just throw your data into the page and it should 'just work' tm.



    I saw you wanted o have an arbitrary number of lists, so allowed for that. Also, presumably, you'd like an arbitrary number of options in each list. What is uncertain however, is what the expected long-term behaviour of such a control would be. I.e if I have two lists, can I only evr expect to see two words in the box, or will it just continue to append the most recently chosen one ad-infinitum?



    So, when the page loads I quickly examine the page's contents, looking for and counting instances of <OL> elements. I then create an array with this many elements before setting an attribute of each list to tell the list what it's index is.



    When an <li> is clicked, the word is retrieved, the index of the list in which it resides is retrieved and then the word is stuffed into the array we created earlier. We then ask for a re-draw, in which case all of the elements of the array are stuck together with a space(' ') in between each element, before it's blasted to the screen.






    "use strict";
    function byId(id)return document.getElementById(id)
    function newEl(tag)return document.createElement(tag)
    window.addEventListener('load', onWindowLoaded, false);

    var wordArray;

    function onWindowLoaded(evt)

    var orderedLists = document.querySelectorAll('ol');
    var listArray = [...orderedLists];
    wordArray = new Array(listArray.length);

    listArray.forEach( function(curList, index)

    curList.dataset.listIndex = index;
    curList.addEventListener('click', onListClicked, false);

    );


    function onListClicked(evt)

    var text = evt.target.textContent;
    var arrayIndex = parseInt(this.dataset.listIndex);
    wordArray[arrayIndex] = text;
    renderText();


    function renderText()

    var str = wordArray.join(" ");
    byId('output').textContent = str;

    	<textarea id='output'></textarea>
    <ol>
    <li>Hello</li>
    <li>Greetings</li>
    <li>Salutations</li>
    </ol>
    <ol>
    <li>Humans</li>
    <li>Peasants</li>
    <li>Earthlings</li>
    </ol>








    share|improve this answer























    • I really like your approach, however; I think OP wanted a running list of the actions taken, should be really easy to update your solution to handle that.

      – Adam H
      Nov 13 '18 at 22:46











    • Actually - I like that this seems to handle only one response from each list. How would you do what @Adam H did with inserting 2 line breaks after each one?

      – Ryan'sDad
      Nov 14 '18 at 13:44











    • @Ryan'sDad - it's actually surprisingly easy. Just change wordArray.join(" "); into wordArray.join("nn"); This way, we join each of the array members together with a pair of new-lines, instead of a single space.

      – enhzflep
      Nov 17 '18 at 0:36















    1














    It seems I've taken a slightly different approach than others used here. I've elected to do everything at run-time, so you don't have to pre-calc anything. Just throw your data into the page and it should 'just work' tm.



    I saw you wanted o have an arbitrary number of lists, so allowed for that. Also, presumably, you'd like an arbitrary number of options in each list. What is uncertain however, is what the expected long-term behaviour of such a control would be. I.e if I have two lists, can I only evr expect to see two words in the box, or will it just continue to append the most recently chosen one ad-infinitum?



    So, when the page loads I quickly examine the page's contents, looking for and counting instances of <OL> elements. I then create an array with this many elements before setting an attribute of each list to tell the list what it's index is.



    When an <li> is clicked, the word is retrieved, the index of the list in which it resides is retrieved and then the word is stuffed into the array we created earlier. We then ask for a re-draw, in which case all of the elements of the array are stuck together with a space(' ') in between each element, before it's blasted to the screen.






    "use strict";
    function byId(id)return document.getElementById(id)
    function newEl(tag)return document.createElement(tag)
    window.addEventListener('load', onWindowLoaded, false);

    var wordArray;

    function onWindowLoaded(evt)

    var orderedLists = document.querySelectorAll('ol');
    var listArray = [...orderedLists];
    wordArray = new Array(listArray.length);

    listArray.forEach( function(curList, index)

    curList.dataset.listIndex = index;
    curList.addEventListener('click', onListClicked, false);

    );


    function onListClicked(evt)

    var text = evt.target.textContent;
    var arrayIndex = parseInt(this.dataset.listIndex);
    wordArray[arrayIndex] = text;
    renderText();


    function renderText()

    var str = wordArray.join(" ");
    byId('output').textContent = str;

    	<textarea id='output'></textarea>
    <ol>
    <li>Hello</li>
    <li>Greetings</li>
    <li>Salutations</li>
    </ol>
    <ol>
    <li>Humans</li>
    <li>Peasants</li>
    <li>Earthlings</li>
    </ol>








    share|improve this answer























    • I really like your approach, however; I think OP wanted a running list of the actions taken, should be really easy to update your solution to handle that.

      – Adam H
      Nov 13 '18 at 22:46











    • Actually - I like that this seems to handle only one response from each list. How would you do what @Adam H did with inserting 2 line breaks after each one?

      – Ryan'sDad
      Nov 14 '18 at 13:44











    • @Ryan'sDad - it's actually surprisingly easy. Just change wordArray.join(" "); into wordArray.join("nn"); This way, we join each of the array members together with a pair of new-lines, instead of a single space.

      – enhzflep
      Nov 17 '18 at 0:36













    1












    1








    1







    It seems I've taken a slightly different approach than others used here. I've elected to do everything at run-time, so you don't have to pre-calc anything. Just throw your data into the page and it should 'just work' tm.



    I saw you wanted o have an arbitrary number of lists, so allowed for that. Also, presumably, you'd like an arbitrary number of options in each list. What is uncertain however, is what the expected long-term behaviour of such a control would be. I.e if I have two lists, can I only evr expect to see two words in the box, or will it just continue to append the most recently chosen one ad-infinitum?



    So, when the page loads I quickly examine the page's contents, looking for and counting instances of <OL> elements. I then create an array with this many elements before setting an attribute of each list to tell the list what it's index is.



    When an <li> is clicked, the word is retrieved, the index of the list in which it resides is retrieved and then the word is stuffed into the array we created earlier. We then ask for a re-draw, in which case all of the elements of the array are stuck together with a space(' ') in between each element, before it's blasted to the screen.






    "use strict";
    function byId(id)return document.getElementById(id)
    function newEl(tag)return document.createElement(tag)
    window.addEventListener('load', onWindowLoaded, false);

    var wordArray;

    function onWindowLoaded(evt)

    var orderedLists = document.querySelectorAll('ol');
    var listArray = [...orderedLists];
    wordArray = new Array(listArray.length);

    listArray.forEach( function(curList, index)

    curList.dataset.listIndex = index;
    curList.addEventListener('click', onListClicked, false);

    );


    function onListClicked(evt)

    var text = evt.target.textContent;
    var arrayIndex = parseInt(this.dataset.listIndex);
    wordArray[arrayIndex] = text;
    renderText();


    function renderText()

    var str = wordArray.join(" ");
    byId('output').textContent = str;

    	<textarea id='output'></textarea>
    <ol>
    <li>Hello</li>
    <li>Greetings</li>
    <li>Salutations</li>
    </ol>
    <ol>
    <li>Humans</li>
    <li>Peasants</li>
    <li>Earthlings</li>
    </ol>








    share|improve this answer













    It seems I've taken a slightly different approach than others used here. I've elected to do everything at run-time, so you don't have to pre-calc anything. Just throw your data into the page and it should 'just work' tm.



    I saw you wanted o have an arbitrary number of lists, so allowed for that. Also, presumably, you'd like an arbitrary number of options in each list. What is uncertain however, is what the expected long-term behaviour of such a control would be. I.e if I have two lists, can I only evr expect to see two words in the box, or will it just continue to append the most recently chosen one ad-infinitum?



    So, when the page loads I quickly examine the page's contents, looking for and counting instances of <OL> elements. I then create an array with this many elements before setting an attribute of each list to tell the list what it's index is.



    When an <li> is clicked, the word is retrieved, the index of the list in which it resides is retrieved and then the word is stuffed into the array we created earlier. We then ask for a re-draw, in which case all of the elements of the array are stuck together with a space(' ') in between each element, before it's blasted to the screen.






    "use strict";
    function byId(id)return document.getElementById(id)
    function newEl(tag)return document.createElement(tag)
    window.addEventListener('load', onWindowLoaded, false);

    var wordArray;

    function onWindowLoaded(evt)

    var orderedLists = document.querySelectorAll('ol');
    var listArray = [...orderedLists];
    wordArray = new Array(listArray.length);

    listArray.forEach( function(curList, index)

    curList.dataset.listIndex = index;
    curList.addEventListener('click', onListClicked, false);

    );


    function onListClicked(evt)

    var text = evt.target.textContent;
    var arrayIndex = parseInt(this.dataset.listIndex);
    wordArray[arrayIndex] = text;
    renderText();


    function renderText()

    var str = wordArray.join(" ");
    byId('output').textContent = str;

    	<textarea id='output'></textarea>
    <ol>
    <li>Hello</li>
    <li>Greetings</li>
    <li>Salutations</li>
    </ol>
    <ol>
    <li>Humans</li>
    <li>Peasants</li>
    <li>Earthlings</li>
    </ol>








    "use strict";
    function byId(id)return document.getElementById(id)
    function newEl(tag)return document.createElement(tag)
    window.addEventListener('load', onWindowLoaded, false);

    var wordArray;

    function onWindowLoaded(evt)

    var orderedLists = document.querySelectorAll('ol');
    var listArray = [...orderedLists];
    wordArray = new Array(listArray.length);

    listArray.forEach( function(curList, index)

    curList.dataset.listIndex = index;
    curList.addEventListener('click', onListClicked, false);

    );


    function onListClicked(evt)

    var text = evt.target.textContent;
    var arrayIndex = parseInt(this.dataset.listIndex);
    wordArray[arrayIndex] = text;
    renderText();


    function renderText()

    var str = wordArray.join(" ");
    byId('output').textContent = str;

    	<textarea id='output'></textarea>
    <ol>
    <li>Hello</li>
    <li>Greetings</li>
    <li>Salutations</li>
    </ol>
    <ol>
    <li>Humans</li>
    <li>Peasants</li>
    <li>Earthlings</li>
    </ol>





    "use strict";
    function byId(id)return document.getElementById(id)
    function newEl(tag)return document.createElement(tag)
    window.addEventListener('load', onWindowLoaded, false);

    var wordArray;

    function onWindowLoaded(evt)

    var orderedLists = document.querySelectorAll('ol');
    var listArray = [...orderedLists];
    wordArray = new Array(listArray.length);

    listArray.forEach( function(curList, index)

    curList.dataset.listIndex = index;
    curList.addEventListener('click', onListClicked, false);

    );


    function onListClicked(evt)

    var text = evt.target.textContent;
    var arrayIndex = parseInt(this.dataset.listIndex);
    wordArray[arrayIndex] = text;
    renderText();


    function renderText()

    var str = wordArray.join(" ");
    byId('output').textContent = str;

    	<textarea id='output'></textarea>
    <ol>
    <li>Hello</li>
    <li>Greetings</li>
    <li>Salutations</li>
    </ol>
    <ol>
    <li>Humans</li>
    <li>Peasants</li>
    <li>Earthlings</li>
    </ol>






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 13 '18 at 21:51









    enhzflepenhzflep

    10.3k22240




    10.3k22240












    • I really like your approach, however; I think OP wanted a running list of the actions taken, should be really easy to update your solution to handle that.

      – Adam H
      Nov 13 '18 at 22:46











    • Actually - I like that this seems to handle only one response from each list. How would you do what @Adam H did with inserting 2 line breaks after each one?

      – Ryan'sDad
      Nov 14 '18 at 13:44











    • @Ryan'sDad - it's actually surprisingly easy. Just change wordArray.join(" "); into wordArray.join("nn"); This way, we join each of the array members together with a pair of new-lines, instead of a single space.

      – enhzflep
      Nov 17 '18 at 0:36

















    • I really like your approach, however; I think OP wanted a running list of the actions taken, should be really easy to update your solution to handle that.

      – Adam H
      Nov 13 '18 at 22:46











    • Actually - I like that this seems to handle only one response from each list. How would you do what @Adam H did with inserting 2 line breaks after each one?

      – Ryan'sDad
      Nov 14 '18 at 13:44











    • @Ryan'sDad - it's actually surprisingly easy. Just change wordArray.join(" "); into wordArray.join("nn"); This way, we join each of the array members together with a pair of new-lines, instead of a single space.

      – enhzflep
      Nov 17 '18 at 0:36
















    I really like your approach, however; I think OP wanted a running list of the actions taken, should be really easy to update your solution to handle that.

    – Adam H
    Nov 13 '18 at 22:46





    I really like your approach, however; I think OP wanted a running list of the actions taken, should be really easy to update your solution to handle that.

    – Adam H
    Nov 13 '18 at 22:46













    Actually - I like that this seems to handle only one response from each list. How would you do what @Adam H did with inserting 2 line breaks after each one?

    – Ryan'sDad
    Nov 14 '18 at 13:44





    Actually - I like that this seems to handle only one response from each list. How would you do what @Adam H did with inserting 2 line breaks after each one?

    – Ryan'sDad
    Nov 14 '18 at 13:44













    @Ryan'sDad - it's actually surprisingly easy. Just change wordArray.join(" "); into wordArray.join("nn"); This way, we join each of the array members together with a pair of new-lines, instead of a single space.

    – enhzflep
    Nov 17 '18 at 0:36





    @Ryan'sDad - it's actually surprisingly easy. Just change wordArray.join(" "); into wordArray.join("nn"); This way, we join each of the array members together with a pair of new-lines, instead of a single space.

    – enhzflep
    Nov 17 '18 at 0:36













    0














    If you want text paragraphs:



    document.getElementById("alltext").value += "t" + (targ.textContent || targ.innerText) + "n";





    <textarea id="alltext"></textarea>
    <ol onclick="addText(event)">
    <li>Hello</li>
    <li>Greetings</li>
    <li>Salutations</li>
    </ol>

    <ol onclick="addText(event)">
    <li>Humans!</li>
    <li>Peasants?</li>
    <li>Earthlings.</li>
    </ol>
    <script>
    function addText(event) event.srcElement;
    document.getElementById("alltext").value += "t" + (targ.textContent
    </script>





    If you want HTML Paragraphs you can change the alltext textarea to a div, and use the following code:



    document.getElementById("alltext").innerHTML += "<p>" + (targ.textContent || targ.innerText) + "</p>";





    <div id="alltext"></div>
    <ol onclick="addText(event)">
    <li>Hello</li>
    <li>Greetings</li>
    <li>Salutations</li>
    </ol>

    <ol onclick="addText(event)">
    <li>Humans!</li>
    <li>Peasants?</li>
    <li>Earthlings.</li>
    </ol>
    <script>
    function addText(event)
    var targ = event.target
    </script>








    share|improve this answer



























      0














      If you want text paragraphs:



      document.getElementById("alltext").value += "t" + (targ.textContent || targ.innerText) + "n";





      <textarea id="alltext"></textarea>
      <ol onclick="addText(event)">
      <li>Hello</li>
      <li>Greetings</li>
      <li>Salutations</li>
      </ol>

      <ol onclick="addText(event)">
      <li>Humans!</li>
      <li>Peasants?</li>
      <li>Earthlings.</li>
      </ol>
      <script>
      function addText(event) event.srcElement;
      document.getElementById("alltext").value += "t" + (targ.textContent
      </script>





      If you want HTML Paragraphs you can change the alltext textarea to a div, and use the following code:



      document.getElementById("alltext").innerHTML += "<p>" + (targ.textContent || targ.innerText) + "</p>";





      <div id="alltext"></div>
      <ol onclick="addText(event)">
      <li>Hello</li>
      <li>Greetings</li>
      <li>Salutations</li>
      </ol>

      <ol onclick="addText(event)">
      <li>Humans!</li>
      <li>Peasants?</li>
      <li>Earthlings.</li>
      </ol>
      <script>
      function addText(event)
      var targ = event.target
      </script>








      share|improve this answer

























        0












        0








        0







        If you want text paragraphs:



        document.getElementById("alltext").value += "t" + (targ.textContent || targ.innerText) + "n";





        <textarea id="alltext"></textarea>
        <ol onclick="addText(event)">
        <li>Hello</li>
        <li>Greetings</li>
        <li>Salutations</li>
        </ol>

        <ol onclick="addText(event)">
        <li>Humans!</li>
        <li>Peasants?</li>
        <li>Earthlings.</li>
        </ol>
        <script>
        function addText(event) event.srcElement;
        document.getElementById("alltext").value += "t" + (targ.textContent
        </script>





        If you want HTML Paragraphs you can change the alltext textarea to a div, and use the following code:



        document.getElementById("alltext").innerHTML += "<p>" + (targ.textContent || targ.innerText) + "</p>";





        <div id="alltext"></div>
        <ol onclick="addText(event)">
        <li>Hello</li>
        <li>Greetings</li>
        <li>Salutations</li>
        </ol>

        <ol onclick="addText(event)">
        <li>Humans!</li>
        <li>Peasants?</li>
        <li>Earthlings.</li>
        </ol>
        <script>
        function addText(event)
        var targ = event.target
        </script>








        share|improve this answer













        If you want text paragraphs:



        document.getElementById("alltext").value += "t" + (targ.textContent || targ.innerText) + "n";





        <textarea id="alltext"></textarea>
        <ol onclick="addText(event)">
        <li>Hello</li>
        <li>Greetings</li>
        <li>Salutations</li>
        </ol>

        <ol onclick="addText(event)">
        <li>Humans!</li>
        <li>Peasants?</li>
        <li>Earthlings.</li>
        </ol>
        <script>
        function addText(event) event.srcElement;
        document.getElementById("alltext").value += "t" + (targ.textContent
        </script>





        If you want HTML Paragraphs you can change the alltext textarea to a div, and use the following code:



        document.getElementById("alltext").innerHTML += "<p>" + (targ.textContent || targ.innerText) + "</p>";





        <div id="alltext"></div>
        <ol onclick="addText(event)">
        <li>Hello</li>
        <li>Greetings</li>
        <li>Salutations</li>
        </ol>

        <ol onclick="addText(event)">
        <li>Humans!</li>
        <li>Peasants?</li>
        <li>Earthlings.</li>
        </ol>
        <script>
        function addText(event)
        var targ = event.target
        </script>








        <textarea id="alltext"></textarea>
        <ol onclick="addText(event)">
        <li>Hello</li>
        <li>Greetings</li>
        <li>Salutations</li>
        </ol>

        <ol onclick="addText(event)">
        <li>Humans!</li>
        <li>Peasants?</li>
        <li>Earthlings.</li>
        </ol>
        <script>
        function addText(event) event.srcElement;
        document.getElementById("alltext").value += "t" + (targ.textContent
        </script>





        <textarea id="alltext"></textarea>
        <ol onclick="addText(event)">
        <li>Hello</li>
        <li>Greetings</li>
        <li>Salutations</li>
        </ol>

        <ol onclick="addText(event)">
        <li>Humans!</li>
        <li>Peasants?</li>
        <li>Earthlings.</li>
        </ol>
        <script>
        function addText(event) event.srcElement;
        document.getElementById("alltext").value += "t" + (targ.textContent
        </script>





        <div id="alltext"></div>
        <ol onclick="addText(event)">
        <li>Hello</li>
        <li>Greetings</li>
        <li>Salutations</li>
        </ol>

        <ol onclick="addText(event)">
        <li>Humans!</li>
        <li>Peasants?</li>
        <li>Earthlings.</li>
        </ol>
        <script>
        function addText(event)
        var targ = event.target
        </script>





        <div id="alltext"></div>
        <ol onclick="addText(event)">
        <li>Hello</li>
        <li>Greetings</li>
        <li>Salutations</li>
        </ol>

        <ol onclick="addText(event)">
        <li>Humans!</li>
        <li>Peasants?</li>
        <li>Earthlings.</li>
        </ol>
        <script>
        function addText(event)
        var targ = event.target
        </script>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 21:25









        zfrischzfrisch

        4,52211024




        4,52211024





















            0














            Here are a couple of possible solutions, you can pick which one you like best.



            Append all to a single textarea with line breaks:






            function addText(event) 
            var targ = event.target

            <textarea id="alltext"></textarea>
            <ol onclick="addText(event)">
            <li>Hello</li>
            <li>Greetings</li>
            <li>Salutations</li>
            </ol>

            <ol onclick="addText(event)">
            <li>Humans!</li>
            <li>Peasants?</li>
            <li>Earthlings.</li>
            </ol>





            Append P tags to a Div:






            var container = document.getElementById('alltext');

            function addText(event)

            <p id="alltext"></p>
            <ol onclick="addText(event)">
            <li>Hello</li>
            <li>Greetings</li>
            <li>Salutations</li>
            </ol>

            <ol onclick="addText(event)">
            <li>Humans!</li>
            <li>Peasants?</li>
            <li>Earthlings.</li>
            </ol>








            share|improve this answer

























            • I really like the first option - but is it possible to add a second line break so that it looks more like the second example? Thank you so much!

              – Ryan'sDad
              Nov 13 '18 at 21:36











            • @Ryan'sDad I updated the first solution for you.

              – Adam H
              Nov 13 '18 at 22:18















            0














            Here are a couple of possible solutions, you can pick which one you like best.



            Append all to a single textarea with line breaks:






            function addText(event) 
            var targ = event.target

            <textarea id="alltext"></textarea>
            <ol onclick="addText(event)">
            <li>Hello</li>
            <li>Greetings</li>
            <li>Salutations</li>
            </ol>

            <ol onclick="addText(event)">
            <li>Humans!</li>
            <li>Peasants?</li>
            <li>Earthlings.</li>
            </ol>





            Append P tags to a Div:






            var container = document.getElementById('alltext');

            function addText(event)

            <p id="alltext"></p>
            <ol onclick="addText(event)">
            <li>Hello</li>
            <li>Greetings</li>
            <li>Salutations</li>
            </ol>

            <ol onclick="addText(event)">
            <li>Humans!</li>
            <li>Peasants?</li>
            <li>Earthlings.</li>
            </ol>








            share|improve this answer

























            • I really like the first option - but is it possible to add a second line break so that it looks more like the second example? Thank you so much!

              – Ryan'sDad
              Nov 13 '18 at 21:36











            • @Ryan'sDad I updated the first solution for you.

              – Adam H
              Nov 13 '18 at 22:18













            0












            0








            0







            Here are a couple of possible solutions, you can pick which one you like best.



            Append all to a single textarea with line breaks:






            function addText(event) 
            var targ = event.target

            <textarea id="alltext"></textarea>
            <ol onclick="addText(event)">
            <li>Hello</li>
            <li>Greetings</li>
            <li>Salutations</li>
            </ol>

            <ol onclick="addText(event)">
            <li>Humans!</li>
            <li>Peasants?</li>
            <li>Earthlings.</li>
            </ol>





            Append P tags to a Div:






            var container = document.getElementById('alltext');

            function addText(event)

            <p id="alltext"></p>
            <ol onclick="addText(event)">
            <li>Hello</li>
            <li>Greetings</li>
            <li>Salutations</li>
            </ol>

            <ol onclick="addText(event)">
            <li>Humans!</li>
            <li>Peasants?</li>
            <li>Earthlings.</li>
            </ol>








            share|improve this answer















            Here are a couple of possible solutions, you can pick which one you like best.



            Append all to a single textarea with line breaks:






            function addText(event) 
            var targ = event.target

            <textarea id="alltext"></textarea>
            <ol onclick="addText(event)">
            <li>Hello</li>
            <li>Greetings</li>
            <li>Salutations</li>
            </ol>

            <ol onclick="addText(event)">
            <li>Humans!</li>
            <li>Peasants?</li>
            <li>Earthlings.</li>
            </ol>





            Append P tags to a Div:






            var container = document.getElementById('alltext');

            function addText(event)

            <p id="alltext"></p>
            <ol onclick="addText(event)">
            <li>Hello</li>
            <li>Greetings</li>
            <li>Salutations</li>
            </ol>

            <ol onclick="addText(event)">
            <li>Humans!</li>
            <li>Peasants?</li>
            <li>Earthlings.</li>
            </ol>








            function addText(event) 
            var targ = event.target

            <textarea id="alltext"></textarea>
            <ol onclick="addText(event)">
            <li>Hello</li>
            <li>Greetings</li>
            <li>Salutations</li>
            </ol>

            <ol onclick="addText(event)">
            <li>Humans!</li>
            <li>Peasants?</li>
            <li>Earthlings.</li>
            </ol>





            function addText(event) 
            var targ = event.target

            <textarea id="alltext"></textarea>
            <ol onclick="addText(event)">
            <li>Hello</li>
            <li>Greetings</li>
            <li>Salutations</li>
            </ol>

            <ol onclick="addText(event)">
            <li>Humans!</li>
            <li>Peasants?</li>
            <li>Earthlings.</li>
            </ol>





            var container = document.getElementById('alltext');

            function addText(event)

            <p id="alltext"></p>
            <ol onclick="addText(event)">
            <li>Hello</li>
            <li>Greetings</li>
            <li>Salutations</li>
            </ol>

            <ol onclick="addText(event)">
            <li>Humans!</li>
            <li>Peasants?</li>
            <li>Earthlings.</li>
            </ol>





            var container = document.getElementById('alltext');

            function addText(event)

            <p id="alltext"></p>
            <ol onclick="addText(event)">
            <li>Hello</li>
            <li>Greetings</li>
            <li>Salutations</li>
            </ol>

            <ol onclick="addText(event)">
            <li>Humans!</li>
            <li>Peasants?</li>
            <li>Earthlings.</li>
            </ol>






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 13 '18 at 22:18

























            answered Nov 13 '18 at 21:26









            Adam HAdam H

            972115




            972115












            • I really like the first option - but is it possible to add a second line break so that it looks more like the second example? Thank you so much!

              – Ryan'sDad
              Nov 13 '18 at 21:36











            • @Ryan'sDad I updated the first solution for you.

              – Adam H
              Nov 13 '18 at 22:18

















            • I really like the first option - but is it possible to add a second line break so that it looks more like the second example? Thank you so much!

              – Ryan'sDad
              Nov 13 '18 at 21:36











            • @Ryan'sDad I updated the first solution for you.

              – Adam H
              Nov 13 '18 at 22:18
















            I really like the first option - but is it possible to add a second line break so that it looks more like the second example? Thank you so much!

            – Ryan'sDad
            Nov 13 '18 at 21:36





            I really like the first option - but is it possible to add a second line break so that it looks more like the second example? Thank you so much!

            – Ryan'sDad
            Nov 13 '18 at 21:36













            @Ryan'sDad I updated the first solution for you.

            – Adam H
            Nov 13 '18 at 22:18





            @Ryan'sDad I updated the first solution for you.

            – Adam H
            Nov 13 '18 at 22:18

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289558%2fappending-text-into-single-text-area-separating-by-paragraph-using-javascript%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto