Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript DOM Scripting By Example Editing and Filtering Names States of the Application

Kevin Nunley
Kevin Nunley
5,373 Points

Why wouldn't we just give the edit and remove buttons classes and check for that?

Why would we check the text content of the button when we could assign classes to the edit and remove buttons and check for the matching class using e.target.className? My code functions exactly the same and is arguably more legible.

Here's some snippets of my code:

From the button declarations:

    const editButton = document.createElement('button');
    editButton.textContent = 'Edit';
    editButton.className = 'editButton'
    li.appendChild(editButton);

    const removeButton = document.createElement('button');
    removeButton.textContent = 'Remove';
    removeButton.className = 'removeButton'
    li.appendChild(removeButton);

Event Handler:

ul.addEventListener('click', (e) => {
    if (e.target.className === 'removeButton'){
        const li = e.target.parentNode;
        const ul = li.parentNode;
        ul.removeChild(li);
    }
    else if (e.target.className === 'editButton'){
        console.log("edit clicked");
    };
});
Aakash Srivastav
seal-mask
.a{fill-rule:evenodd;}techdegree
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 Points

What you are saying is right. You can do that too i,e give different classes to 'edit' and 'remove' buttons and check for it in event handler . But , you can also use '.textContent' property too.
Now , if you are giving them classes just to compare them within the event handler so that they can be differentiated, then it would be better to use '.textContent' property . So ,it just a matter of situation :)
You can use whatever method suits you .
Both the methods are right All the best :)

1 Answer

Aakash Srivastav
seal-mask
.a{fill-rule:evenodd;}techdegree
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 Points

What you are saying is right. You can do that too i,e give different classes to 'edit' and 'remove' buttons and check for it in event handler . But , you can also use '.textContent' property too.
Now , if you are giving them classes just to compare them within the event handler so that they can be differentiated, then it would be better to use '.textContent' property . So ,it just a matter of situation :)
You can use whatever method suits you .
Both the methods are right All the best :)

Kevin Nunley
Kevin Nunley
5,373 Points

Thank you for your response! After giving this some more thought, I came to a similar conclusion. Although, I can see comparing based on classes to be useful if the site you are building has to be localized and the button text is in a different language.