0

To clarify the question, let me give the example like below.
I have three <div> elements like below:

  <div id="fail-item" class="toolbar-item">
  Fail
  </div>
  <div id="warn-item" class="toolbar-item">
  Warn
  </div>
  <div id="pass-item" class="toolbar-item">
  Pass
  </div>

I added click listener for each <div> listed with Jquery like below:

<body>
  <script>
    $("div.toolbar-item").click(toolbar_click_listener());

    function toolbar_click_listener(){
        console.log("what happens");
    }   
  </script>
</body>

However, I can see "what happens" printed only once when I load the page, any further click event to those <div> elements doesn't work at all.

I searched over SO but failed to get answers. Do you know what's wrong here?

Eugene
  • 10,627
  • 5
  • 49
  • 67
  • 3
    Should be `$("div.toolbar-item").click(toolbar_click_listener)`, without parentheses. Written like that, you _call_ the given function - and this happens exactly once. – raina77ow Apr 24 '18 at 07:15
  • 2
    Use `$("div.toolbar-item").click(toolbar_click_listener);` – Tan Duong Apr 24 '18 at 07:16
  • 1
    When you clicked more after 1st click, did you notice the numbers increasing in brackets in Console of browser? Actually the console of browser clubs the same messages or events and just shows the counter. – Himanshu Upadhyay Apr 24 '18 at 07:18

4 Answers4

3

Use without parenthesis:

$("div.toolbar-item").click(toolbar_click_listener);

Also you can easily do this by

$("div.toolbar-item").click(function(){
  console.log("what happens");
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Nirali
  • 1,776
  • 2
  • 12
  • 23
3

It should be a function reference in handler not function call.

Your click handler should not be called on page load. It's happening because you are calling it here .click(toolbar_click_listener()); You will not want that.

When you want to assign a handler to an event. You give reference to that function not call it.

Change it to .click(toolbar_click_listener); and you'll see the expected behavior

$("div.toolbar-item").click(toolbar_click_listener);

    function toolbar_click_listener(){
        console.log("what happens");
    }  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="fail-item" class="toolbar-item">
  Fail
  </div>
  <div id="warn-item" class="toolbar-item">
  Warn
  </div>
  <div id="pass-item" class="toolbar-item">
  Pass
  </div>
Muhammad Usman
  • 10,039
  • 22
  • 39
1

Just pass only reference of toolbar_click_listener function in click.

<body>
  <script>
    $("div.toolbar-item").click(toolbar_click_listener);

    function toolbar_click_listener(){
        console.log("what happens");
    }   
  </script>
</body>
Nishant Dixit
  • 5,388
  • 5
  • 17
  • 29
0

Working Fiddle

HTML:

 <div id="fail-item" class="toolbar-item">
  Fail
  </div>
  <div id="warn-item" class="toolbar-item">
  Warn
  </div>
  <div id="pass-item" class="toolbar-item">
  Pass
  </div>

JS:

$("div.toolbar-item").on("click",toolbar_click_listener);

    function toolbar_click_listener(){
        console.log("what happens");
    } 
Minar Mnr
  • 1,376
  • 1
  • 16
  • 23