Skip to content

Instantly share code, notes, and snippets.

@Franckapik
Last active January 21, 2018 17:19
Show Gist options
  • Save Franckapik/f622628cfb804afa482bba63bd7ea5b3 to your computer and use it in GitHub Desktop.
Save Franckapik/f622628cfb804afa482bba63bd7ea5b3 to your computer and use it in GitHub Desktop.
Multiple ID function to one
document.getElementById("color-1").addEventListener("click", function() {
if (_previous_selected_obj) {
colorize("color-1", "mat1");
modeleTable[_previous_selected_obj.origin_name.substring(6, 7)] = "N1";
} else {
notify("[WARNING]");
}
}, false);
HTML
<div class="option-content">
<div id="color-1" class="circle">
</div>
<div id="color-2" class="circle">
</div>
<div id="color-3" class="circle">
</div>
@efdee
Copy link

efdee commented Jan 21, 2018

document.getElementById("color-1").addEventListener("click", handleClick, false);
document.getElementById("color-2").addEventListener("click", handleClick, false);
document.getElementById("color-3").addEventListener("click", handleClick, false);

function handleClick(event) {
      var id = event.currentTarget.id;
      if (_previous_selected_obj) {
        colorize(id, "mat1");
        modeleTable[_previous_selected_obj.origin_name.substring(6, 7)] = "N1";
      } else {
        notify("[WARNING]");
      }
}

@mpalmr
Copy link

mpalmr commented Jan 21, 2018

var optionContentElement = document.querySelector('.option-content');
var selectedId = null;

optionContentElement.addEventListener('click', function (event) {
  var isCircle = event.target.classList.contains('circle');
  var isSelected = event.target.id === selectedId;

  if (isCircle && isSelected) {
    event.stopPropagation();
    selectedId = event.target.id;
    colorize(selectedId, 'mat1');
    modeleTable[selectedId].origin_name.substring(6, 7)] = 'N1';
  } else notify('[WARNING]');
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment