4

i have a code snippet like this.

$(document).on("click", event_target, some_function);

DEFAULT BEHAVIOUR:

If i pass event_target as null or empty string, all clicks on the document are handled.

DESIRED BEHAVIOUR

I want that if event_target is an empty string the event shouldn't be handled.

CURRENT APPROACH

Now i just add a if statement in the handler and if target is document i simply do nothing.

Is there a better approach to avoid unnecessarily calling the handler for all clicks?

2
  • 3
    I don't think so... what is wrong with your current approach Commented Jun 9, 2015 at 3:33
  • @ArunPJohny Well i thought i wanted to avoid the overhead of calling the handler everytime i click or tap somtehing in the entire document Commented Jun 9, 2015 at 4:00

1 Answer 1

2

Why don't you just do what you're currently doing inside an if block? Set up the click handler only if event_target is not an empty string.

if (event_target !== '')
    $(document).on("click", event_target, some_function);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.