3
Objective: Get the contents of <li>
, play in the text field, change, return pressing Enter and clear the field.
Upshot: The closest I can get is to take the content, edit it in the text field, return it in the list but I can’t clear the field without some problem in a <li>
.
Question: It is possible to do this using this code?
My current code:
let lista = document.querySelector('.lista');
let campo = document.querySelector('.campo');
lista.addEventListener('click',function(event){
let y = event.target;
campo.value = y.textContent;
campo.addEventListener('keypress',function(event){
if(event.keyCode === 13) {
y.textContent = event.target.value;
y = '';
};
});
});
<ul class="lista">
<li class="lista__item">um</li>
<li class="lista__item">dois</li>
<li class="lista__item">tres</li>
</ul>
<input class="campo" type="text" name="" id="">
Speak, Sam! I just ran your code and debugged it. The same error happened to the other, if you select the Li and click enter it passes the value, but if I click enter again, without having changed the target by clicking another read, then it passes the blank field to the li, changing again. For this, followed the same principle of the other code, added li = '' at the end of the event field.addeventlistener('keypress',Function(Event) and ran perfectly. Anyway, both proposals helped me a lot, thank you very much!!!
– Diluxa
I changed the code just before your comment.
– Sam
Putz, perfect!!! Thank you so much for having taken some of your time in helping.
– Diluxa