Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
198
rated 0 times [  199] [ 1]  / answers: 1 / hits: 34707  / 9 Years ago, tue, november 24, 2015, 12:00:00

I am working on making a To-Do list as a Chrome new tab extension.



My html file:



<head>
</head>

<body>
<h2 id=toc-final>To Do List</h2>
<ul id=todoItems></ul>
<input type=text id=todo name=todo placeholder=What do you need to do? style=width: 200px;>
<button id=newitem value=Add Todo Item>Add</button>

<script type=text/javascript src=indexdb.js></script>
</body>
</html>


Previously the button element was an input type with onClick(), but Chrome does not allow that. So I had to make a javascript function that will fire when it's clikced. In my indexdb.js:



var woosToDo = {};
window.indexedDB = window.indexedDB || window.webkitIndexedDB ||
window.mozIndexedDB;

woosToDo.indexedDB = {};
woosToDo.indexedDB.db = null;

window.addEventListener(DOMContentLoaded, init, false);

window.addEventListener('DOMContentLoaded', function () {
document.getElementById(newitem).addEventListener(click, addTodo(), false);
});

...
...

function addTodo() {
var todo = document.getElementById(todo);
woosToDo.indexedDB.addTodo(todo.value);
todo.value = ;
}


Why is nothing happening when I click the button w/ id=newitem ?


More From » html

 Answers
4

When attaching the function, you are executing it first, and attaching the return value undefined to the event. Remove the parenthesis:



.addEventListener(click, addTodo, false);


When you put addTodo() as a parameter, you are not passing the function itself. The first thing it does is execute the function and use the return value as the parameter instead.



Since functions without a return statement implicitly result in undefined, the original code was actually running the function, and then attaching this:



.addEventListener(click, undefined, false);

[#64281] Sunday, November 22, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aidan

Total Points: 72
Total Questions: 95
Total Answers: 121

Location: Uzbekistan
Member since Sat, Feb 27, 2021
3 Years ago
aidan questions
Mon, Oct 11, 21, 00:00, 3 Years ago
Wed, Sep 29, 21, 00:00, 3 Years ago
Sun, Sep 5, 21, 00:00, 3 Years ago
Thu, Jan 16, 20, 00:00, 4 Years ago
;