How can i improve my code

Hello, I am new to coding and just got started with HTML and CSS. I wanted to know how I can improve my code and any options and suggestions. I wanted to write the best code.

This is my first project—a ToDo app.

<!DOCTYPE html>
<html>
<body>

    <h1>Have Something in your mind!</h1>
    <hr>
    <br>
    <div class="center">
            
            <input id="noteinput" type="text" placeholder="What's in your mind? ">
            <button id="addbtn">Keep Note</button>
    </div>

    <ul id="notelist"></ul>
     <h4 id="ErrorMessage" style="color:red; display:none; text-align: center;">
        Please write something first!
    </h4>
    <hr>

    <style>
        h1{
            text-align: center;
            font-family: Arial, Helvetica, sans-serif;
        }

        .center{
            display: flex;
            justify-content: center; 
            align-items: center; 
        }

        #noteinput{
            width: 550px;
            height: 60px;
            outline: none;
            border: none;
        }

        li{
            text-align: center;
            font-family: Arial, Helvetica, sans-serif;

        }

        li:hover {
             background: #eaeaea;
}

    </style>

<script>
    const input = document.getElementById("noteinput");
    const button = document.getElementById("addbtn");
    const list = document.getElementById("notelist");
    const error = document.getElementById("ErrorMessage");

    button.addEventListener("click", () => {
        if (input.value.trim() === "") {
            error.style.display = "block";
            return; 
        }

        error.style.display = "none";

        const li = document.createElement("li");
        li.textContent = input.value;

        li.addEventListener("click", () => {
            li.remove();
        });

        list.appendChild(li);
        input.value = "";
    });
</script>

</body>
</html>

Thank you.

1 Like

Welcome to the forum @unicornasset!

And congratulations for wanting to improve your code!

I recommend working through the Responsive Web Design curriculum offered here at Responsive Web Design Certification | freeCodeCamp.org. There you will learn how to separate your HTML, CSS, and JavaScript into separate files and link your CSS and JavaScript files in your HTML file.

To start, maybe have a look at the HTML boilerplate lecture:

Using an HTML boilerplate to start your projects is a great way to make sure your HTML has a solid foundation.

Welcome to the forum @unicornasset

Awesome todo app!

  • Perhaps change the title text to “Do you have something on your mind?”
  • If you do the above, then you can change the button text to “Save idea”
  • Place the thoughts and ideas between the horizontal rules.
  • Place the warning message below the text input.
  • The style element goes in the head element.
  • Use selectors to style the error message instead of inline styling.
  • Use bold text instead of an h4 element, to avoid messing with heading hierarchy.

Happy coding

1 Like

did everything you provided, thank you happy new years!

note: I put error message below the list

1 Like