skip to Main Content

I am trying to display a string containing a list of items in a specific format inside a div tag using JavaScript. The string contains several items that I want to display as a bulleted list. Here’s the example string:

const items = "These are the items:n1. applen2. mango";

I want to format the string so that it appears as follows inside the div tag:

These are the items:
1. apple
2. mango

I am using React and Tailwind CSS, and this question pertains to a React component.

3

Answers


  1. const string = "These are the items:n1. applen2. mango"; console.log(string.split("n"));

    Login or Signup to reply.
  2. You should parse your string in order to have a list.
    A very simple parse would be :
    items.split('/n')

    and you would receive

    ["These are the items:", "1. apple", "2. mango"]
    which is now easy to display

    Login or Signup to reply.
  3. You can pass it as innerText:

    const items = "These are the items:n1. applen2. mango";
    document.getElementById("thediv").innerText=items;
    <div id="thediv"></div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search