function changeColor(index) {
return function(e) {
e.target.style.backgroundColor = "black";
console.log('index: ', index);
}
}
function qs(selector) {
return document.querySelectorAll(selector);
}
qs('.block').forEach(function(el, i) {
el.addEventListener('click', changeColor(i), false);
});
qs('.boxer').forEach(function(el, i) {
el.addEventListener('click', changeColor(i), false);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$('.block').click(function(){
$(this).css({'backgroundColor' : 'black'});
var myClass = $(this).attr('class');
var ind = $('.'+myClass).index(this);
console.log(myClass);
if (ind) console.log(ind);
});
</script>