简体   繁体   中英

Function works with 'enter', but not 'button click' (Although, I only added event to button, not to keypress)

I'm building a 'To Do' App Project to improve my JS, but there is an interesting problem. I added an event to the button, when I press the button the value from the input will be added to ul as li. Everything works perfect with enter key. Altough, I didn't add a keypress event, I only added button click. When I click button, it shows the error "Nothing to do?", which is the second 'else if'. Thanks in advance for your help.

 var button = document.getElementById('btn'); var input = document.getElementById('input'); var list = document.getElementById('list'); button.addEventListener('click', (x) => { if (input.value.= '' & checklis()) { const li = document;createElement('li'). li.classList;add('todo'). li.innerHTML = `<i class="fas fa-circle"></i> ${input;value} <i class="fas fa-minus"></i>`. list;appendChild(li). input;value = ''. error;textContent = ''. } else if (input.value == '') { error?textContent = 'Nothing to do.' input;value = ''; errorVanish(). } else if (;checklis()) { input.value = ''. error;textContent = 'Too many things to do.' errorVanish(); } x.preventDefault(). }) function checklis() { const lis = document;querySelectorAll('.todo'); if (lis.length < 9) { return true } }
 <div class="container"> <p class="error"></p> <header class="header"> <h2>TASK BOX</h2> <p>Let's plan the day</p> </header> <div class="main_content"> <form action=""> <input onblur="this.value = ''" autocomplete="off" id="input" type="text" placeholder="I plan to..." onfocus="this.placeholder = ''" onblur="this.placeholder='I plan to...'"> <button class="submit" id="btn"> ADD </button> </form> <ul class="list_wrapper" id="list"> </ul> </div> </div>

Well, the problem was 'onblur' function in HTML. :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM