JSFiddle does not manage click events inside a functionWhat does the exclamation mark do before the function?Is there a download function in jsFiddle?jsFiddle: no connection between html and js? Can't call simple function from button?Why does preventDefault() of a click event also prevent my form from submitting?Anchor tag's onclick isn't executing a simple jQuery function in jsfiddleJQuery not executing functions, works in JSFiddle but not WordpressWhat specifically will successfully trigger a JavaScript click event in JSFiddle?jsfiddle identical(?) fiddles with button to change text one works one does notgetorgchart - click event occuring twiceHow to stop event propagation in jQuery click function inside click function?

How to train a replacement without them knowing?

Which manga depicts Doraemon and Nobita on Easter Island?

What does a comma signify in inorganic chemistry?

Representing an indicator function: binary variables and "indicator constraints"

Do predators tend to have vertical slit pupils versus horizontal for prey animals?

From where do electrons gain kinetic energy through a circuit?

Does knowing that the exponent is in a certain range help solving discrete log?

Why does this image of cyclocarbon look like a nonagon?

Quick destruction of a helium filled airship?

Output with the same length always

What if a restaurant suddenly cannot accept credit cards, and the customer has no cash?

Programming a recursive formula into Mathematica and find the nth position in the sequence

Combinatorial Argument for Exponential and Logarithmic Function Being Inverse

What was the intention with the Commodore 128?

Subgroup generated by a subgroup and a conjugate of it

Will some rockets really collapse under their own weight?

Rotate List by K places

How do I answer an interview question about how to handle a hard deadline I won't be able to meet?

Number of matrices with bounded products of rows and columns

Is a suspension needed to do wheelies?

A reccomended structured approach to self studying music theory for songwriting

Which basis does the wavefunction collapse to?

Does the Temple of the Gods spell nullify critical hits?

How do the Durable and Dwarven Fortitude feats interact?



JSFiddle does not manage click events inside a function


What does the exclamation mark do before the function?Is there a download function in jsFiddle?jsFiddle: no connection between html and js? Can't call simple function from button?Why does preventDefault() of a click event also prevent my form from submitting?Anchor tag's onclick isn't executing a simple jQuery function in jsfiddleJQuery not executing functions, works in JSFiddle but not WordpressWhat specifically will successfully trigger a JavaScript click event in JSFiddle?jsfiddle identical(?) fiddles with button to change text one works one does notgetorgchart - click event occuring twiceHow to stop event propagation in jQuery click function inside click function?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















I have a very simple JSFiddle



HTML



<input type="button" value="Submit" id="myBtn" onclick="foo()">


JS



function foo()
console.log('click');

$("#myBtn").click(function(event)
console.log('click2');
);

console.log('click3');




Here's the link: https://jsfiddle.net/mdvyo481/



I've already loaded the script in No Wrap - Head mode. Strangely, when I click on the button, click and click3 are printed in console but click2 is not printed. If I click on it again, I get 2 prints of click2.



Looks like event is not properly managed in the fiddle. Is there a way to fix?










share|improve this question



















  • 1





    Nothing to do with JSFiddle, your code is wrong. It will behave exactly the same way in a browser.

    – Robin Zigmond
    Mar 27 at 13:32






  • 2





    To be more constructive, your event handler foo directly logs click and click3, and inbetween simply adds another click handler, which will log click2. So you won't see that until the next time you click it. And each subsequent click will add another handler, therefore click2 will get logged more and more times. Basically, you should never add event listeners (for the same event) inside an event handler.

    – Robin Zigmond
    Mar 27 at 13:33


















2















I have a very simple JSFiddle



HTML



<input type="button" value="Submit" id="myBtn" onclick="foo()">


JS



function foo()
console.log('click');

$("#myBtn").click(function(event)
console.log('click2');
);

console.log('click3');




Here's the link: https://jsfiddle.net/mdvyo481/



I've already loaded the script in No Wrap - Head mode. Strangely, when I click on the button, click and click3 are printed in console but click2 is not printed. If I click on it again, I get 2 prints of click2.



Looks like event is not properly managed in the fiddle. Is there a way to fix?










share|improve this question



















  • 1





    Nothing to do with JSFiddle, your code is wrong. It will behave exactly the same way in a browser.

    – Robin Zigmond
    Mar 27 at 13:32






  • 2





    To be more constructive, your event handler foo directly logs click and click3, and inbetween simply adds another click handler, which will log click2. So you won't see that until the next time you click it. And each subsequent click will add another handler, therefore click2 will get logged more and more times. Basically, you should never add event listeners (for the same event) inside an event handler.

    – Robin Zigmond
    Mar 27 at 13:33














2












2








2








I have a very simple JSFiddle



HTML



<input type="button" value="Submit" id="myBtn" onclick="foo()">


JS



function foo()
console.log('click');

$("#myBtn").click(function(event)
console.log('click2');
);

console.log('click3');




Here's the link: https://jsfiddle.net/mdvyo481/



I've already loaded the script in No Wrap - Head mode. Strangely, when I click on the button, click and click3 are printed in console but click2 is not printed. If I click on it again, I get 2 prints of click2.



Looks like event is not properly managed in the fiddle. Is there a way to fix?










share|improve this question














I have a very simple JSFiddle



HTML



<input type="button" value="Submit" id="myBtn" onclick="foo()">


JS



function foo()
console.log('click');

$("#myBtn").click(function(event)
console.log('click2');
);

console.log('click3');




Here's the link: https://jsfiddle.net/mdvyo481/



I've already loaded the script in No Wrap - Head mode. Strangely, when I click on the button, click and click3 are printed in console but click2 is not printed. If I click on it again, I get 2 prints of click2.



Looks like event is not properly managed in the fiddle. Is there a way to fix?







javascript jsfiddle






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 27 at 13:31









floatingpurrfloatingpurr

2,1015 gold badges18 silver badges56 bronze badges




2,1015 gold badges18 silver badges56 bronze badges










  • 1





    Nothing to do with JSFiddle, your code is wrong. It will behave exactly the same way in a browser.

    – Robin Zigmond
    Mar 27 at 13:32






  • 2





    To be more constructive, your event handler foo directly logs click and click3, and inbetween simply adds another click handler, which will log click2. So you won't see that until the next time you click it. And each subsequent click will add another handler, therefore click2 will get logged more and more times. Basically, you should never add event listeners (for the same event) inside an event handler.

    – Robin Zigmond
    Mar 27 at 13:33













  • 1





    Nothing to do with JSFiddle, your code is wrong. It will behave exactly the same way in a browser.

    – Robin Zigmond
    Mar 27 at 13:32






  • 2





    To be more constructive, your event handler foo directly logs click and click3, and inbetween simply adds another click handler, which will log click2. So you won't see that until the next time you click it. And each subsequent click will add another handler, therefore click2 will get logged more and more times. Basically, you should never add event listeners (for the same event) inside an event handler.

    – Robin Zigmond
    Mar 27 at 13:33








1




1





Nothing to do with JSFiddle, your code is wrong. It will behave exactly the same way in a browser.

– Robin Zigmond
Mar 27 at 13:32





Nothing to do with JSFiddle, your code is wrong. It will behave exactly the same way in a browser.

– Robin Zigmond
Mar 27 at 13:32




2




2





To be more constructive, your event handler foo directly logs click and click3, and inbetween simply adds another click handler, which will log click2. So you won't see that until the next time you click it. And each subsequent click will add another handler, therefore click2 will get logged more and more times. Basically, you should never add event listeners (for the same event) inside an event handler.

– Robin Zigmond
Mar 27 at 13:33






To be more constructive, your event handler foo directly logs click and click3, and inbetween simply adds another click handler, which will log click2. So you won't see that until the next time you click it. And each subsequent click will add another handler, therefore click2 will get logged more and more times. Basically, you should never add event listeners (for the same event) inside an event handler.

– Robin Zigmond
Mar 27 at 13:33













2 Answers
2






active

oldest

votes


















2














This is correct behaviour. The reason this happens is because you bind the event click when you call the foo function. The first time you call foo you bind the event, the second time you bind it again and so on. Binding an event doesn't remove the previous bind, so if you click submit 10 times, you will bind 10 different click eventhandlers, all doing the same thing and you'll receive 'click2' 10 times in the console.






share|improve this answer
































    1














    Either add a click handler on your button using jquery or by using the onclick attribute, not both.



    I've provided two examples to illustrate this:






    function foo(element) 
    console.log(element.id + ' was clicked <- onclick attribute handler')



    $("#myBtn").click(function(event)
    console.log(event.target.id + ' was clicked, <- jQuery handler')
    )

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <input type="button" value="Submit" id="myBtn" onclick="foo(this)">





    Reason for your example not working is that you are setting the jquery click handler only after calling foo(), so it will not also trigger.






    share|improve this answer



























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55378466%2fjsfiddle-does-not-manage-click-events-inside-a-function%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      This is correct behaviour. The reason this happens is because you bind the event click when you call the foo function. The first time you call foo you bind the event, the second time you bind it again and so on. Binding an event doesn't remove the previous bind, so if you click submit 10 times, you will bind 10 different click eventhandlers, all doing the same thing and you'll receive 'click2' 10 times in the console.






      share|improve this answer





























        2














        This is correct behaviour. The reason this happens is because you bind the event click when you call the foo function. The first time you call foo you bind the event, the second time you bind it again and so on. Binding an event doesn't remove the previous bind, so if you click submit 10 times, you will bind 10 different click eventhandlers, all doing the same thing and you'll receive 'click2' 10 times in the console.






        share|improve this answer



























          2












          2








          2







          This is correct behaviour. The reason this happens is because you bind the event click when you call the foo function. The first time you call foo you bind the event, the second time you bind it again and so on. Binding an event doesn't remove the previous bind, so if you click submit 10 times, you will bind 10 different click eventhandlers, all doing the same thing and you'll receive 'click2' 10 times in the console.






          share|improve this answer













          This is correct behaviour. The reason this happens is because you bind the event click when you call the foo function. The first time you call foo you bind the event, the second time you bind it again and so on. Binding an event doesn't remove the previous bind, so if you click submit 10 times, you will bind 10 different click eventhandlers, all doing the same thing and you'll receive 'click2' 10 times in the console.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 27 at 13:34









          Vladimir BogomolovVladimir Bogomolov

          1,0687 silver badges13 bronze badges




          1,0687 silver badges13 bronze badges


























              1














              Either add a click handler on your button using jquery or by using the onclick attribute, not both.



              I've provided two examples to illustrate this:






              function foo(element) 
              console.log(element.id + ' was clicked <- onclick attribute handler')



              $("#myBtn").click(function(event)
              console.log(event.target.id + ' was clicked, <- jQuery handler')
              )

              <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
              <input type="button" value="Submit" id="myBtn" onclick="foo(this)">





              Reason for your example not working is that you are setting the jquery click handler only after calling foo(), so it will not also trigger.






              share|improve this answer





























                1














                Either add a click handler on your button using jquery or by using the onclick attribute, not both.



                I've provided two examples to illustrate this:






                function foo(element) 
                console.log(element.id + ' was clicked <- onclick attribute handler')



                $("#myBtn").click(function(event)
                console.log(event.target.id + ' was clicked, <- jQuery handler')
                )

                <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                <input type="button" value="Submit" id="myBtn" onclick="foo(this)">





                Reason for your example not working is that you are setting the jquery click handler only after calling foo(), so it will not also trigger.






                share|improve this answer



























                  1












                  1








                  1







                  Either add a click handler on your button using jquery or by using the onclick attribute, not both.



                  I've provided two examples to illustrate this:






                  function foo(element) 
                  console.log(element.id + ' was clicked <- onclick attribute handler')



                  $("#myBtn").click(function(event)
                  console.log(event.target.id + ' was clicked, <- jQuery handler')
                  )

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                  <input type="button" value="Submit" id="myBtn" onclick="foo(this)">





                  Reason for your example not working is that you are setting the jquery click handler only after calling foo(), so it will not also trigger.






                  share|improve this answer













                  Either add a click handler on your button using jquery or by using the onclick attribute, not both.



                  I've provided two examples to illustrate this:






                  function foo(element) 
                  console.log(element.id + ' was clicked <- onclick attribute handler')



                  $("#myBtn").click(function(event)
                  console.log(event.target.id + ' was clicked, <- jQuery handler')
                  )

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                  <input type="button" value="Submit" id="myBtn" onclick="foo(this)">





                  Reason for your example not working is that you are setting the jquery click handler only after calling foo(), so it will not also trigger.






                  function foo(element) 
                  console.log(element.id + ' was clicked <- onclick attribute handler')



                  $("#myBtn").click(function(event)
                  console.log(event.target.id + ' was clicked, <- jQuery handler')
                  )

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                  <input type="button" value="Submit" id="myBtn" onclick="foo(this)">





                  function foo(element) 
                  console.log(element.id + ' was clicked <- onclick attribute handler')



                  $("#myBtn").click(function(event)
                  console.log(event.target.id + ' was clicked, <- jQuery handler')
                  )

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                  <input type="button" value="Submit" id="myBtn" onclick="foo(this)">






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 27 at 13:38









                  WebberWebber

                  1,36313 silver badges17 bronze badges




                  1,36313 silver badges17 bronze badges






























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55378466%2fjsfiddle-does-not-manage-click-events-inside-a-function%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                      은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현