HTML Academy
Summary of “Actions with DOM”
Working with DOM23/23
Back to the list of tasks
  • 1. What are you planning to do?
  • 2. The “change” event
  • 3. How the “change” event works
  • 4. Deleting the element from the list
  • 5. How to check the collection length
  • 6. Debugging the code
  • 7. Static and live collections
  • 8. Displaying the message on the page
  • 9. The “submit” event
  • 10. Cancelling the form submission
  • 11. How to obtain text from the input field
  • 12. Templates and the <template> tag
  • 13. The contents of the <template> tag, document-fragment
  • 14. Cloning and inserting elements, part 1
  • 15. Cloning and inserting elements, part 2
  • 16. Cloning and inserting elements, part 3
  • 17. Cloning and inserting elements, part 4
  • 18. How to clone elements
  • 19. Adding a new element to a list
  • 20. Deleting a new task in the list
  • 21. Clearing the input field
  • 22. Summary of “Actions with DOM”
  • 23. The third program: “Instant messenger”
List of tasks
  • Sign up
  • Log in

Register to take up challenges

Registration will only take a minute and let you save your study progress. You can register with your email and password or login via social networks.

or
Log in and continue
  • index.html
  • style.css
  • script.js
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Chat</title> <link href="style.css" rel="stylesheet"> </head> <body> <section class="chat"> <h1 class="visually-hidden">Chat</h1> <div class="chat-content"> </div> <form class="chat-form" action="https://echo.htmlacademy.ru/courses" method="post"> <input class="chat-form-input" type="text" aria-label="Your message" placeholder="Write something" required> <button class="chat-form-button" type="submit">Submit</button> </form> </section> <template id="message-template"> <div class="chat-message" tabindex="0"> <span class="chat-message-name">Unidentified raccoon</span> <p class="chat-message-text"></p> <button class="chat-message-button" type="button">Delete</button> </div> </template> <script src="script.js"></script> </body> </html>
CSS
@font-face { font-weight: 400; font-family: "Muller"; font-style: normal; font-display: swap; src: url("fonts/Muller.woff") format("woff"); } html, body { height: 100%; } body { padding: 0; margin: 0; min-width: 420px; font-size: 16px; line-height: 20px; font-family: "Muller", sans-serif; } .visually-hidden { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; white-space: nowrap; border: 0; clip: rect(0 0 0 0); clip-path: inset(100%); } .chat { display: flex; flex-direction: column; height: 100%; } .chat-content { display: grid; align-content: end; align-items: end; justify-content: end; flex-grow: 1; padding: 15px 20px 24px 30px; overflow: auto; background-color: #5c8ab3; background-image: url("pattern.svg"); background-repeat: repeat; } .chat-message { position: relative; padding: 3px 20px 11px; background-color: #ffffff; box-shadow: 0 4px 4px rgba(0, 0, 0, 0.15); border-radius: 20px 4px 4px 20px; max-width: 425px; } .chat-message:hover .chat-message-button, .chat-message:focus .chat-message-button { visibility: visible; } .chat-message:focus-within .chat-message-button { visibility: visible; } .chat-message-text { margin: 0; } .chat-message-name { display: inline-block; margin-bottom: 2px; font-size: 12px; line-height: 16px; color: #6994ba; } .chat-message-button { position: absolute; width: 18px; height: 18px; top: -5px; right: -5px; padding: 0; visibility: hidden; background-color: #ffffff; background-image: url("icon-cross.svg"); background-repeat: no-repeat; background-position: center; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.25); border: none; border-radius: 50%; font: inherit; font-size: 0; user-select: none; touch-action: manipulation; cursor: pointer; } .chat-message + .chat-message { margin-top: 12px; } .chat-message:last-child { border-radius: 20px 4px 20px 20px; } .chat-message:first-child { border-radius: 20px 20px 4px 20px; } .chat-form { display: flex; align-items: center; padding: 32px 20px; background-color: #265681; } .chat-form-input { flex-grow: 1; box-sizing: border-box; margin-right: 16px; padding: 9px 20px 11px; border: 2px solid #ffffff; background-color: #ffffff; } .chat-form-input::placeholder { color: #cccccc; } .chat-form-input:hover { border-color: #56cf4c; outline: none; } .chat-form-input:focus { border-color: #9edcff; outline: none; } .chat-form-input, .chat-form-button { border-radius: 50px; font: inherit; } .chat-form-button { flex-shrink: 0; padding: 9px 20px; color: #ffffff; background-color: #39b54a; box-shadow: 0 4px 4px rgba(0, 0, 0, 0.15); border: 2px solid transparent; user-select: none; outline: none; touch-action: manipulation; cursor: pointer; } .chat-form-button::before { content: ""; display: inline-block; vertical-align: middle; width: 19px; height: 20px; margin-right: 6px; background-image: url("icon-plane.svg"); background-repeat: no-repeat; } .chat-form-button:hover { background-color: #56cf4c; box-shadow: 0 8px 4px rgba(0, 0, 0, 0.15); } .chat-form-button:focus { border-color: 2px solid #9edcff; } .chat-form-button:active { background-color: #208b44; box-shadow: none; }
JavaScript
/* You need to program an instant messaging app. It must have the following functionality: — The message template is in the template tag with the message-template ID. — The message block (with the chat-message class) must contain the message text, a delete button, and a username. — The new message is added to the end of the container with the chat-content class. — To add a new message, you need to enter text in the input field (an element with the chat-form-input class) and click the “Submit” button (which sends data from the form with the chat-form class). - To delete the message, you need to click the cross button (an element with the chat-message-button class). This button appears when you hover the mouse over a message. */

What didn’t you like in this task?

Thanks! We’ll fix everything at once!

Click inside the mini browser to put the focus in this window.

100%
Console
var chatContainer = document.querySelector('.chat-content'); var newMessageForm = document.querySelector('.chat-form'); var newMessageInput = newMessageForm.querySelector('.chat-form-input'); var messageTemplate = document.querySelector('#message-template').content; var newMessageTemplate = messageTemplate.querySelector('.chat-message'); var addClickHandler = function (element, button) { button.addEventListener('click', function () { element.remove(); }); }; newMessageForm.addEventListener('submit', function (evt) { evt.preventDefault(); var messageText = newMessageInput.value; var newMessage = newMessageTemplate.cloneNode(true); var newMessageText = newMessage.querySelector('.chat-message-text'); newMessageText.textContent = messageText; var deleteButton = newMessage.querySelector('.chat-message-button'); addClickHandler(newMessage, deleteButton); chatContainer.appendChild(newMessage); newMessageInput.value = ''; });

Cookies ∙ Privacy ∙ License Agreement ∙ About ∙ Contacts ∙ © HTML Academy OÜ, 2019−2025

VISAMastercard

Log in

or

Forgot your password?

Sign up

Sign up

or
Log in

Restore access

Have you forgotten your password or lost access to your profile? Enter your email connected to your profile and we will send you a link to restore access.

Forgot to connect your email to the profile? Email us and we’ll help.