How can I can make this JavaScript code cleaner?

I’m new to JS and I did this exercise for the course I’m taking. It’s basically done, but I had to repeat the code that creates the "delete" button and I’m not sure how I can make this cleaner.

var button = document.getElementById("add")
var input = document.getElementById("userinput")
var ul = document.querySelector("ul")
var li = document.querySelectorAll("li")

function inputLength() {
    return input.value.length;

function createListElement() {
    let li = document.createElement("li")
    input.value = ""

    // Create a delete button and configure it:
    var btnDelete = document.createElement("button");
    btnDelete.textContent = "Delete";

    // Append the button to the list item

// Create a delete button for each <li> already in the HTML file
    var btnDelete = document.createElement("button");
    btnDelete.textContent = "Delete";

function addListAfterClick() {
    if(inputLength() > 0) {

function addListAfterKeypress(event) {
    if(inputLength() > 0 && event.keyCode === 13) {

button.addEventListener("click", addListAfterClick)

input.addEventListener("keypress", addListAfterKeypress)

function toggleClassDoneOnAndOff(event) {
    if ( === "LI") {"done");

ul.addEventListener("click", toggleClassDoneOnAndOff);

function deleteAfterClick(event) {
    // Determine if it was a delete button that was clicked
      // Remove the closest li ancestor to the clicked element"li").remove();

// Handle all the clicks that originate from within the <ul> at
// the <ul> level when they bubble up to it instead of setting each
// button within the <ul> up with its own click event handler.
ul.addEventListener("click", deleteAfterClick);
.done {
    text-decoration: line-through;
    <h1>Shopping List</h1>
    <p id="first">Get it done today</p>
    <input id="userinput" type="text" placeholder="Add items">
    <button id="add">Add</button>
    <ul id="ul">
        <li>Birthday Cake</li>
    <script type="text/javascript" src="script.js"></script>

My question is that simple, but since Stack Overflow is telling me to "add some more details"…

Basically, I tried to turn this part into a function:

var btnDelete = document.createElement("button");
btnDelete.textContent = "Delete";

But I couldn’t figure out how to make it work inside the other ones.

80 thoughts on “How can I can make this JavaScript code cleaner?”

  1. I got this website from my buddy who informed me
    regarding this website and now this time I am visiting this website and reading very informative articles at this

  2. I’m really inspired along with your writing abilities as well as with the structure to your
    weblog. Is this a paid theme or did you customize
    it yourself? Either way stay up the nice quality writing,
    it’s uncommon to look a nice blog like this one these days..

  3. It’s a shame you don’t have a donate button! I’d without
    a doubt donate to this outstanding blog! I guess for now i’ll settle for
    book-marking and adding your RSS feed to my Google account.
    I look forward to new updates and will talk about this site
    with my Facebook group. Chat soon!

  4. We stumbled over here by a different web address
    and thought I should check things out. I like what I see
    so now i’m following you. Look forward to looking into your web page yet

  5. I’m really loving the theme/design of your blog.
    Do you ever run into any browser compatibility problems?
    A couple of my blog visitors have complained about my website not working correctly
    in Explorer but looks great in Chrome. Do you have any suggestions to help
    fix this issue? scoliosis surgery scoliosis surgery


Leave a Comment