1
\$\begingroup\$

I have the following function which has the required functionality. Is there any way that I can reduce the code but have the same functionality?

addClickEventListeners = () => {
  let multipleChoiceQuestionButtons = document.querySelectorAll('[name="multiple_choice"]');
  let trueFalseQuestionButtons = document.querySelectorAll('[name="true_false"]');
  let multipleChoiceMultiAnswersQuestionButtons = document.querySelectorAll('[name="multi_answers"]');
  if (multipleChoiceQuestionButtons.length > 0) {
    multipleChoiceQuestionButtons.forEach((button) => {
      button.addEventListener('click', someFunction, false);
    });
  } else if (trueFalseQuestionButtons.length > 0) {
    trueFalseQuestionButtons.forEach((button) => {
      button.addEventListener('click', someFunction, false);
    });
  } else if (multipleChoiceMultiAnswersQuestionButtons.length > 0) {
    multipleChoiceMultiAnswersQuestionButtons.forEach((button) => {
      button.addEventListener('click', someFunction, false);
    });
  }
};
\$\endgroup\$
2
  • \$\begingroup\$ To be clear, you call the same someFunction in all three cases? \$\endgroup\$
    – konijn
    Commented Feb 18, 2020 at 15:22
  • \$\begingroup\$ Yes. It's the same function. \$\endgroup\$
    – Lax_Sam
    Commented Feb 18, 2020 at 15:25

1 Answer 1

1
\$\begingroup\$

Interesting question;

You can make this code slightly less efficient (however, the user will never notice), and so we can just attach listeners to everything.

const addClickEventListeners = () => {

  const addListener = (button) => button.addEventListener('click', someFunction, false);

  document.querySelectorAll('[name="multiple_choice"]').forEach(addListener);
  document.querySelectorAll('[name="true_false"]').forEach(addListener); 
  document.querySelectorAll('[name="multi_answers"]').forEach(addListener); 

};

This should behave the same way as far as I can tell.

You could also create your buttons that require someFunction with a css class dedicated for that selection or even better with a data attribute for that purpose. Then you just query on that and attach the listener.

\$\endgroup\$
4
  • \$\begingroup\$ Can you please tell why is the above code less efficient? \$\endgroup\$
    – Lax_Sam
    Commented Feb 18, 2020 at 18:08
  • \$\begingroup\$ Because it will try a forEach(), even if nothing was found \$\endgroup\$
    – konijn
    Commented Feb 18, 2020 at 18:21
  • \$\begingroup\$ Since they all attach to the same handler could you comma separate the selectors in one querySelectorAll? Of course keeping it separate can help with readability \$\endgroup\$ Commented Feb 18, 2020 at 21:54
  • \$\begingroup\$ @AdamTaylor totally, you could and I did consider it. \$\endgroup\$
    – konijn
    Commented Feb 19, 2020 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.