Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dom1 #1028

Closed
wants to merge 1 commit into from
Closed

dom1 #1028

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 83 additions & 12 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
<!DOCTYPE html>
<html>
<html lang="ru">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Проект "Комменты"</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="styles.css" />
<style>
.error {
border: 2px solid red;
}
</style>
</head>

<body>
<div class="container">
<ul class="comments">
<ul class="comments" id="commentsList">
<li class="comment">
<div class="comment-header">
<div>Глеб Фокин</div>
Expand Down Expand Up @@ -46,26 +51,92 @@
</ul>
<div class="add-form">
<input
id="inpName"
type="text"
class="add-form-name"
placeholder="Введите ваше имя"
/>
<textarea
id="inpText"
type="textarea"
class="add-form-text"
placeholder="Введите ваш коментарий"
placeholder="Введите ваш комментарий"
rows="4"
></textarea>
<div class="add-form-row">
<button class="add-form-button">Написать</button>
<button id="btn" class="add-form-button">Написать</button>
</div>
</div>
</div>
</body>

<script>
"use strict";
// Код писать здесь
console.log("It works!");
</script>
<script>
const inputName = document.getElementById('inpName')
const inputText = document.getElementById('inpText')
const buttonAdd = document.getElementById('btn')
const commentsList = document.getElementById('commentsList')

function formatDate(date) {
const day = String(date.getDate()).padStart(2, '0')
const month = String(date.getMonth() + 1).padStart(2, '0')
const year = String(date.getFullYear()).slice(-2)
const hours = String(date.getHours()).padStart(2, '0')
const minutes = String(date.getMinutes()).padStart(2, '0')
return `${day}.${month}.${year} ${hours}:${minutes}`
}

function addComment() {
const name = inputName.value.trim()
const text = inputText.value.trim()

inputName.classList.remove('error')
inputText.classList.remove('error')

let isValid = true

if (name === '') {
inputName.classList.add('error')
isValid = false
}

if (text === '') {
inputText.classList.add('error')
isValid = false
}

if (!isValid) {
return
}

const currentDate = new Date()
const dateString = formatDate(currentDate)

const newComment = `
<li class="comment">
<div class="comment-header">
<div>${name}</div>
<div>${dateString}</div>
</div>
<div class="comment-body">
<div class="comment-text">
${text}
</div>
</div>
<div class="comment-footer">
<div class="likes">
<span class="likes-counter">0</span>
<button class="like-button"></button>
</div>
</div>
</li>
`

commentsList.innerHTML += newComment

inputName.value = ''
inputText.value = ''
}

buttonAdd.addEventListener('click', addComment)
</script>
</body>
</html>
Loading