DEV Community

Cover image for πŸ‘¨β€πŸ’» Daily Code 40 | Rendering πŸŸ₯ HTML with 🟨 JS
Gregor Schafroth
Gregor Schafroth

Posted on

πŸ‘¨β€πŸ’» Daily Code 40 | Rendering πŸŸ₯ HTML with 🟨 JS

πŸ™‹β€β™‚οΈ Hi everyone!

Today I continued following the free SuperSimpleDev course and learned how to generate HTML with JS. This is so cool, now I suddenly realize how many websites are made that have complicated repeating elements!

My Code

The actual project I made in that course is just a todo list. Nothing special. But rendering the HTML is just cool πŸ™‚ - Oh yea also now we separated the JavaScript and saved it in a separate file

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Todo List</title>
</head>

<body>
    <p>Todo List</p>
    <input placeholder="Todo name" class="js-name-input">
    <button onclick="addTodo();">Add</button>
    <div class="js-todo-list"></div>
    <script src="scripts/11-todo.js"></script>
</body>

</html>
Enter fullscreen mode Exit fullscreen mode
const todoList = ['make dinner', 'wash dishes'];

renderTodoList();

function renderTodoList() {
    let todoListHTML = '';

    for (let i = 0; i < todoList.length; i++) {
        const todo = todoList[i];
        const html = `<p>${todo}</p>`; // 'generating the HTML'
        todoListHTML += html;
    }
    console.log(todoListHTML);

    document.querySelector('.js-todo-list').innerHTML = todoListHTML;
}

function addTodo() {
    const inputElement = document.querySelector('.js-name-input');
    const name = inputElement.value;

    todoList.push(name);
    console.log(todoList);

    inputElement.value = ''

    renderTodoList();
}
Enter fullscreen mode Exit fullscreen mode

Heroku

Deliver your unique apps, your own way.

Heroku tackles the toil β€” patching and upgrading, 24/7 ops and security, build systems, failovers, and more. Stay focused on building great data-driven applications.

Learn More

Top comments (0)

Neon image

Next.js applications: Set up a Neon project in seconds

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started β†’

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere β€œthank you” often brightens someone’s dayβ€”share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay