The first word in the textarea should enter class "word1" with append tag ‘li’, then the second word should enter class "word2" with tag ‘li’ and the other words in the same order
if no more word from textarea class ol should be emtty
<!DOCTYPE html>
<head>
</head>
<textarea class="rebaz"rows=4 cols=50>hello my name is rebaz</textarea>
<button onclick="show();">Show content</button>
<ol class = "word1"></ol>
<ol class = "word2"></ol>
<ol class = "word3"></ol>
<ol class = "word4"></ol>
<ol class = "word5"></ol>
<ol class = "word6"></ol>
</body>
</html>
after click button
<ol class = "word1"><li>hello</li></ol>
<ol class = "word2"><li>my</li></ol>
<ol class = "word3"><li>name</li></ol>
<ol class = "word4"><li>is</li></ol>
<ol class = "word5"><li>rebaz</li></ol>
<ol class = "word6"></ol>
4
Answers
You may have a javascript function that will split words from the textarea value and will create a new
li
element for each one of them to be appended to the correspondingol
element selected by its class.I should point out that if the textarea contains more than 6 words, the 7th and counting will be ignored. That’s by design according to your requirements. I should also warn that doing lists with single items is pretty meaningless. Just as a reminder, ol stands for ordered list and li stands for list item.
You can do
insertAdjacentHTML
You can use javascript’s trim, split, getElementsByClassName, and appendChild methods to solve this. The
trim
method is useful for removing the extra whitespace in the textarea.In the show method, you can first grab the textarea tag by-
There getElementsByClassName gives an array type data structure. So, you need to write
[0]
to get the first element. After grabbing the textarea you can trim the text inside it and split it based on space (‘ ‘).It will return an array containing words. These words can be inserted into the ol tag.
The show function can be written as-
You can use a single ol tag and multiple li tags inside it. The li tag will represent each of the words. I think it will make more sense instead of using multiple ol tags.
The HTML can be written as-
I hope this logic solve your problem please try it: