I am a student and currently learning jquery <– I am trying to make the function that will take input from the inputbox and then add it to the unordered list as an EXTRA
Using jQuery create an input and a button. When clicking on the button it should invoke a function addToList that will use the input’s value to add it to the toDos variable. Make sure to render it on the screen as a new list item in the unordered list.
JavaScript
x
24
24
1
const body = $("body");
2
const header = $("<header>Todo List</header>");
3
const unorderedList = $("<ul>unorderedlist</ul>");
4
const testButton = $("<button>testButton</button>")
5
const inputBox = $("<input></input>")
6
var toDos = ["wake up", "eat breakfast", "code"];
7
8
9
$("<ul>")
10
.append(toDos.map((text) => $("<li>", { text })))
11
.appendTo(document.body);
12
13
testButton.on("click", () => {
14
console.log("hello");
15
.append(inputBox.text) => $("<li>", { text })
16
.appendTo(document.body);
17
});
18
19
20
body.append(header);
21
body.append(unorderedList);
22
body.append(inputBox);
23
body.append(testButton);
24
Advertisement
Answer
You had several problems which I fixed:
JavaScript
1
21
21
1
const body = $("body");
2
const header = $("<header>Todo List</header>");
3
const unorderedList = $("<ul></ul>");
4
const testButton = $("<button>testButton</button>")
5
const inputBox = $("<input></input>")
6
var toDos = ["wake up", "eat breakfast", "code"];
7
8
// Add toDos to the <ul>
9
unorderedList.append(toDos.map((text) => $("<li>", { text })))
10
11
// Add click handler to button:
12
testButton.on("click", () => {
13
console.log("hello");
14
unorderedList.append($("<li>", { text: inputBox.val() }))
15
});
16
17
body.append(header);
18
body.append(unorderedList);
19
body.append(inputBox);
20
body.append(testButton);
21