hey guys doing this tutorial again on JavaScript. I noticed that when I click the button I don't see any text after I enter in the new items. Do any of you know why. The border is there from my css so I know its being entered in, anyway here is my code so far.
<!doctype html>
<html>
<head>
<title>To do list with html and javascript</title>
<style>
ul { list-style: none; padding: 0; margin: 0; width: 400px;}
li { border: 4px solid #ccc; background: #eee; padding: 10px 15px; color: #000; }
</style>
</head>
<body>
<h1>To Do List</h1>
<p><button id="btnAdd">click here!</button></p>
<ul id="todolist">
</ul>
</ul>
<script src="todo.js"></script>
</body>
</html>
and
function addNewItem(list, itemText){
var listItem = document.createElement("li");
listItem.innerText = "itemText";
list.appendChild(listItem);
}
var btnNew= document.getElementById("btnAdd");
btnNew.onclick = function() {
var itemText = prompt("what is your task?")
addNewItem(document.getElementById("todolist"), itemText);
};
Your html mark up is wrongly posted . There is an extra ul
there.
More over the js code must be like :
function addNewItem(list, itemText){
var listItem = document.createElement("li");
listItem.innerText = itemText;
list.appendChild(listItem);
}
var btnNew= document.getElementById("btnAdd");
btnNew.onclick = function() {
var itemText = prompt("what is your task?")
addNewItem(document.getElementById("todolist"), itemText);
};
Next find the working solution in this fiddle : http://jsfiddle.net/LRyKd/1/
When you give itemText
as 'itemText'
then itemText becomes a string and that constant string gets appended instead of the text you are entering. That's why it was not working for you .
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다