Disclaimer: I am sure that someone somewhere asked these question, but I couldn't think of the accurate google key words to locate similar questions. So please bear with me.
Question: I am constantly suffering from the necessity to re-write entire bulks of JQuery code because of the following pattern:
<div class = 'foo'></div>
<script>
$('.foo').on('click', function(){
// Do something
});
</script>
The problem with this code is that it's too fragile. If I decide to rename my class to change the structure of the DOM, entire bulks of JS related to binding handlers to DOM elemenets are subject to editing.
What is the best-practice to make JQuery code less prone to such kind of risk or ideally eliminate it at all?