0

I am trying to make a function that will render the toDo items as list items in the unorderedList

Create a function renderList that renders the todo items from toDos as list items in the unordered list, make sure that the function is dynamic and that it gets invoked at when while loading the page

How do yo do this in Jquery?

const body = $("body");
const header =$("<header>Todo List</header>")
const unorderedList = $("<ul>unorderedlist</ul>")
var toDos = ["wake up","eat breakfast","code"];

/*const renderList function()=>{
       
{
*/
body.append(header)
body.append(unorderedList)
body.append(toDos);
Adam.M
  • 57
  • 7

2 Answers2

1

You can map your array to a list of elements and append them to the list

const toDos = ["wake up", "eat breakfast", "code"];

$("<ul>") // create a <ul>
  .append(toDos.map(text => $("<li>", { text }))) // create <li> elements and append them
  .appendTo(document.body) // append the <ul> to <body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

To explain this part...

$("<li>", { text })

// or more verbosely
$("<li>", { text: text })

// or even more verbosely
$("<li>").text(text)

See https://api.jquery.com/jquery/#jQuery-html-attributes. It's creating a new <li> element and setting the text property to the current value in the toDos array.


For completeness, here's the you might not need jQuery version

const createElement = (tag, props = {}) =>
  Object.assign(document.createElement(tag), props)

const toDos = ["wake up", "eat breakfast", "code"];

const ul = createElement("ul")
ul.append(...toDos.map(textContent => createElement("li", { textContent })))

document.body.append(ul)
Phil
  • 157,677
  • 23
  • 242
  • 245
  • first of all thank you very much for the answer! May I ask you: How does it know when you write $("
      ") which ul to choose, what if i had multiple in the code ? How does this function start and finish, what if I wrote code below it, is the code a part of it ? or does it end after you append
    – Adam.M Aug 06 '21 at 03:25
  • When you use a full tag string (ie `"
      "` as opposed to `"ul"`), jQuery creates a new element for you. If you wanted to select a particular element that was already in the document, you would just use a selector like `$("#id-of-element")`
    – Phil Aug 06 '21 at 03:27
  • 1
    Also how do you read this part (text => $("
  • ", { text }) ? sorry I am just trying to understand and I dont like to copy paste ^_^
  • – Adam.M Aug 06 '21 at 03:27
  • @Adam.M I've updated my answer with an explanation of that part – Phil Aug 06 '21 at 03:31
  • Thank you Phil, you are amazing, I am loving Jquery by the minute, just hate that its very shortened to the point where its kinda hard to understand whats going on when you see the code for the first time And not to mention iam use to curly brackets which tells me hey, we started here we end here. in jquery i dont see how i can know start and end unless i just start using space between code – Adam.M Aug 06 '21 at 03:35
  • @Adam.M I think you might be referring to jQuery's method chaining. Have a read of this post ~ [how does jquery chaining work?](https://stackoverflow.com/questions/7475336/how-does-jquery-chaining-work) – Phil Aug 06 '21 at 03:37
  • @Adam.M also, with modern browsers there's a very good chance [you might not need jQuery](http://youmightnotneedjquery.com/). I've added a vanilla JS version for comparison – Phil Aug 06 '21 at 03:42