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 Improving the Application Code Refactor 2: Readable Branching Logic

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

Is using a textContent string to make branching decisions a questionable practice?

Am I missing something or did Guil only use textContent for branching to keep things simple?

To me, Guil's approach of creating a "readable" branching logic style which depends upon textContent of the button to make a branching decision creates a problem for anyone wanting to write multilingual websites.

Wouldn't it be better to be using element ID or element class name to discern which button received the event?

I think it makes the code less readable and maintainable.. also, the constant values should be kept separately in const variables.

2 Answers

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 :)
But , overall , its good to give them classes or id and compare with them . You can use whatever method suits you .
Both the methods are right . All the best :)

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

Aakash -- thanks for the response.

I am a JavaScript novice, but have been involved in some coding activities in other languages that developed interesting "quirks" when dealing with unicode strings displayed in the GUI. This experience seemed to suggest that GUI and "business rules" be kept as separate as possible!