HTML Academy
How to obtain text from the input field
Working with DOM12/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”
The contents of the <template> tag, document-fragment
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Templates and the <template> tag

We have the data that we need to create a new task. We know when we can add the task to the page. All that we have left to do is to figure out exactly how we can add a new element.

We have already created new elements using createElement, but this method is long. First we need to create an element and assign it a class and attributes. Then we need to create child elements and to embed them in their parent element, and finally we have to draw them on the page.

It would be convenient if all of the required markup for the future elements was already stored somewhere. All that we have left to do is tweak the content for each element. And this can be done using the template tag.

It contains the template for future elements. The template tag is located in the same place where all the rest of the site markup is, only its content is not displayed on the page. Our markup also contains the template. It stores the template markup of the new task.

In order to obtain the template in JavaScript, you can search for it by tag name, such as, for example, using querySelector('template'). There is a downside to this method: there might be a lot of templates on the page. Usually each template tag is assigned a unique name, and they are written to the id (identifier) attribute. The values of this attribute cannot be repeated on the same page. You can find the desired template by its id.

The template in the markup:

<body>
  …
  <template id="text-template">
      <p class="text"></p>
  </template>
</body>

Searching for an element in JavaScript:

document.querySelector('#text-template');

The hashtag in the querySelector parameter indicates that you need to search by id.

Our template has the task-template ID. Find this template and output the tag contents to the page.

There is also another method in the DOM API that allows you to search for elements by their ID: getElementById. In contrast to other methods, it can be called only across the entire document. It cannot be called for individual elements. We will learn more about this method in future courses when we practice searching for elements by id using querySelector. If you would rather not wait to learn more about the new method, you can read about it on MDN.

Comments

  • index.html
  • style.css
  • script.js
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>The To-Do List</title> <link href="style.css" rel="stylesheet"> </head> <body> <h1 class="visually-hidden">The To-Do List</h1> <ul class="todo-list"> </ul> <p class="empty-tasks">All of the tasks have been completed. There are no new tasks.</p> <form class="add-form" action="https://echo.htmlacademy.ru/courses" method="post"> <input class="add-form-input" type="text" aria-label="Task description" placeholder="For example, buy an elephant" required> <button class="add-form-button" type="submit">Add new task</button> </form> <template id="task-template"> <li class="todo-list-item"> <label> <input type="checkbox" class="todo-list-input"> <span></span> </label> </li> </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"); } body { margin: 40px 32px; font-size: 18px; line-height: normal; font-family: "Muller", sans-serif; } .visually-hidden, .todo-list-input { 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%); } .hidden { display: none; } .todo-list { margin: 0; padding: 0; list-style: none; } .todo-list-item { margin-bottom: 12px; } .todo-list-item label { display: block; padding: 12px 18px; background-color: #ffffff; border-radius: 4px; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.15); cursor: pointer; user-select: none; } .todo-list-item span { display: flex; align-items: center; } .todo-list-input + span::before { content: ""; display: inline-block; width: 16px; height: 16px; margin-right: 16px; background-color: #ffffff; border: 2px solid #69b253; border-radius: 4px; } .todo-list-input:checked + span::before { background-color: #69b253; background-image: url("check-white.svg"); background-repeat: no-repeat; background-position: center; } .todo-list-input:not(:checked):hover + span::before, .todo-list-input:not(:checked):focus + span::before { background-color: rgba(105, 178, 83, 0.2); } .empty-tasks { color: #69b253; } .empty-tasks::before { content: ""; width: 14px; height: 13px; margin-right: 13px; background-image: url("check-green.svg"); background-repeat: no-repeat; background-position: center; } .add-form { display: flex; } .add-form-input { flex-grow: 1; box-sizing: border-box; height: 46px; margin-right: 16px; padding: 12px; font: inherit; border: 1px solid #a1b5c4; border-radius: 4px; } .add-form-input:focus { border: 1px solid #69b253; outline: none; } .add-form-input:hover { box-shadow: 0 0 6px rgba(105, 178, 83, 0.6); } .add-form-input::placeholder { color: #a1b5c4; } .add-form-button { flex-shrink: 0; padding: 12px; font: inherit; color: #ffffff; background-color: #69b253; border: none; border-radius: 4px; user-select: none; touch-action: manipulation; } .add-form-button:hover, .add-form-button:focus { background-color: #5aa045; outline: none; } .add-form-button:active { background-color: #42862e; }
JavaScript
var list = document.querySelector('.todo-list'); var items = list.children; var emptyListMessage = document.querySelector('.empty-tasks'); var newItemForm = document.querySelector('.add-form'); var newItemTitle = newItemForm.querySelector('.add-form-input'); // Declare a variable taskTemplate here var toggleEmptyListMessage = function () { if (items.length === 0) { emptyListMessage.classList.remove('hidden'); } }; var addCheckHandler = function (item) { var checkbox = item.querySelector('.todo-list-input'); checkbox.addEventListener('change', function () { item.remove(); toggleEmptyListMessage(); }); }; for (var i = 0; i < items.length; i++) { addCheckHandler(items[i]); } newItemForm.addEventListener('submit', function (evt) { evt.preventDefault(); var taskText = newItemTitle.value; });

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
Goalscompleted
0
    1. After the newItemTitle variable, declare a variable taskTemplate and write the element with the task-template ID to it.
    2. On the next line, output taskTemplate to the console.

    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.