Appending text into single text area - separating by paragraph (using javascript)
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
add a comment |
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
By paragraph do you mean Text paragraph, or separated by a paragraph tag? Right nowalltext
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 useString.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 appendp
tags to adiv
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
add a comment |
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
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
javascript
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 nowalltext
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 useString.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 appendp
tags to adiv
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
add a comment |
By paragraph do you mean Text paragraph, or separated by a paragraph tag? Right nowalltext
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 useString.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 appendp
tags to adiv
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
add a comment |
3 Answers
3
active
oldest
votes
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>
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 changewordArray.join(" ");
intowordArray.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
add a comment |
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>
add a comment |
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>
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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>
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 changewordArray.join(" ");
intowordArray.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
add a comment |
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>
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 changewordArray.join(" ");
intowordArray.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
add a comment |
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>
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>
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 changewordArray.join(" ");
intowordArray.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
add a comment |
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 changewordArray.join(" ");
intowordArray.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
add a comment |
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>
add a comment |
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>
add a comment |
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>
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>
answered Nov 13 '18 at 21:25
zfrischzfrisch
4,52211024
4,52211024
add a comment |
add a comment |
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>
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
add a comment |
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>
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
add a comment |
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>
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>
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 useString.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 adiv
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