0

Before adding $('.dropdown-toggle').click, I want to make sure there is no callback bound to dropdown-toggle. I am wondering how can I check?

$( document ).ready(function() {
        $('.dropdown-toggle').click(function (e) {
        console.log("aaa")
        $(this).parent().parent().children().eq(1).toggle()
    });

I checked other post, I tried:

if ($('.dropdown-toggle').attr("onClick") != undefined)


$('.dropdown-toggle').click == undefined

But, it doesn't work.

Can I get some help? Thanks!

1 Answer 1

1

Something like that should work.

hasClickListener = function(el) {
  let listeners = $._data(el[0], "events")

  return (listeners !== undefined && listeners.click !== null) || el.prop("onclick") !== null;
}

$(document).ready(function() {

  // false
  console.log(hasClickListener($("#click_here")));

  $("#click_here").click(function() {
    // Do something
  });

  // true
  console.log(hasClickListener($("#click_here")));

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="click_here">Click here</div>

Sign up to request clarification or add additional context in comments.

1 Comment

If you modify the return line you can cover the onclick handlers aren't jquery event handlers: return (listeners !== undefined && listeners.click !== null) || el.prop("onclick") !== null;

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.