JavaScript DOM Scripting By Example Improving the Application Code Refactor 2: Readable Branching Logic

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 23,332 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

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 23,332 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!