Абсолютный начинающий программист здесь. Я пытаюсь создать поле комментариев, в котором все, что вы вводите в комментариях, будет храниться в другом div. Я заставил некоторые из них работать, но я смог добавить их, но они исчезли сразу после этого. Я хочу, чтобы он сохранял комментарии в блоке #comment-box, а когда вы вводите другой комментарий, он сохраняет его внизу. Вот мой код до сих пор
<div class = "container">
<h2>Leave us a comment</h2>
<form>
<textarea id = "" placeholder = "Add Your Comment" value = " "></textarea>
<div class = "btn">
<input id = "submit" type = "submit" value = "Comment">
<button id = "clear">
🙏</button>
</div>
</form>
</div>
<div class = "comments">
<h2>Comments</h2>
<div id = "comment-box" value = "submit">
</div>
</div>
и мой JS
const field = document.querySelector('textarea');
const backUp = field.getAttribute('placeholder')
const btn = document.querySelector('.btn');
const clear = document.getElementById('clear')
const submit = document.querySelector('#submit')
// const comments = document.querySelector('#comment-box')
const comments = document.getElementById('comment-box')
field.onfocus = function(){
this.setAttribute('placeholder','')
this.style.borderColor = '#333'
btn.style.display = 'block'
} // when clicking on this, placeholder changes into ' '.
field.onblur = function(){
this.setAttribute('placeholder',backUp)
} //click away, placeholder returns
clear.onclick = function(){
btn.style.display = 'none';
field.value = ' '
submit.onclick = function(){
submit.style.display = 'none';
const content = document.createTextNode(field.value)
comments.appendChild(content)
Где я ошибаюсь, ребята? Любая обратная связь будет оценена. Благодарю вас
Он исчезает, потому что форма отправляется. Итак, во-первых, вы можете предотвратить это. См. stackoverflow.com/questions/8664486/…
Ага. Как говорит @Shirshak55, поскольку у вас нет базы данных, он печатает комментарий, но также обновляет страницу и сбрасывает все с самого начала.
@james В основном все, что вы вводите в текстовую область, как только вы нажимаете кнопку отправки, все, что вы ввели, добавляется в поле комментария, а когда вы вводите другое, оно сохраняется под первым и так далее.
@Shirshak55 Попробую, спасибо
@PeterLy, хорошо, простое решение, замените кнопку с type=submit на type=button, type=submit используется для отправки данных на сервер, но ваш код кажется чисто клиентским
@James Это правильно, без серверной/внутренней стороны, для этой задачи только передняя часть



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


event.preventDefault();, чтобы предотвратить отправку формы на другую страницуconst field = document.querySelector('textarea');
const backUp = field.getAttribute('placeholder')
const btn = document.querySelector('.btn');
const clear = document.getElementById('clear')
const submit = document.querySelector('#submit')
// const comments = document.querySelector('#comment-box')
const comments = document.getElementById('comment-box');
// array to store the comments
const comments_arr = [];
// to generate html list based on comments array
const display_comments = () => {
let list = '<ul>';
comments_arr.forEach(comment => {
list += `<li>${comment}</li>`;
})
list += '</ul>';
comments.innerHTML = list;
}
clear.onclick = function(event){
event.preventDefault();
// reset the array
comments_arr.length = 0;
// re-genrate the comment html list
display_comments();
}
submit.onclick = function(event){
event.preventDefault();
const content = field.value;
if (content.length > 0){ // if there is content
// add the comment to the array
comments_arr.push(content);
// re-genrate the comment html list
display_comments();
// reset the textArea content
field.value = '';
}
}<div class = "container">
<h2>Leave us a comment</h2>
<form>
<textarea id = "" placeholder = "Add Your Comment" value = " "></textarea>
<div class = "btn">
<input id = "submit" type = "submit" value = "Comment">
<button id = "clear">
🙏</button>
</div>
</form>
</div>
<div class = "comments">
<h2>Comments</h2>
<div id = "comment-box">
</div>
</div>Большое спасибо за помощь. Теперь я могу добавить к этому свой собственный стиль. Очень признателен
Типовая ошибка: пропустить два
}. Кроме того, чего вы хотите достичь? Как ты могenter another comment