JSFiddle - React, Tailwind, and code Playground

by Amanda Williamson

HTML

<button id="a" class="btn">A</button>
<button id="b" class="btn">B</button>

JavaScript

var someObjWithMethods = {
    a: function (){
        console.log('A was clicked');
    },
    b: function (){
        console.log('B was clicked');
    }
};

$('.btn').click(function(){
    //these two do the same thing
    someObjWithMethods.a()
    someObjWithMethods['a']();
    //so does this
    //someObjWithMethods['a' or 'b'}
   someObjWithMethods[$(this).attr('id')]();
});