jQuery Pass Custom Parameter to Common .click() Handler












0














I have a Click-handler defined which handles clicks on an LI menu item. Suppose the code highlights the active item.



$('.menu li').click(function () {
$(this).siblings().removeClass('active');
$(this).addClass('active');
// etc.
}


On startup, I also simulate a click by calling this handler manually. This is required to highlight an item coming from a server-side URL, which will populate hidden fields. The LI Items have ID's so I manually invoke .click() on them.



$('document').ready(function () {
if($("#queueFilter").val() == "MENU1") {
$("#menu1").click();
} else if($("#queueFilter").val() == "MENU2") {
$("#menu2").click();
} else {
$("#menu3").click();
}
}


Problem: I need to distinguish the simulated startup click() invocation from the real user-initiated one. I thought of passing a custom param somehow, but it's not working. Is it possible to specify a custom handler param, which will be NULL in the other case?



Ideally what I want is, on startup:



$("#menu1").click(false); // Indicates a simulated click



Then in the handler definition:



$('.menu li').click(customarg, function () { .. }));



A custom arg would allow me to distinguish in my handler code where I'm coming from and proceed accordingly.










share|improve this question



























    0














    I have a Click-handler defined which handles clicks on an LI menu item. Suppose the code highlights the active item.



    $('.menu li').click(function () {
    $(this).siblings().removeClass('active');
    $(this).addClass('active');
    // etc.
    }


    On startup, I also simulate a click by calling this handler manually. This is required to highlight an item coming from a server-side URL, which will populate hidden fields. The LI Items have ID's so I manually invoke .click() on them.



    $('document').ready(function () {
    if($("#queueFilter").val() == "MENU1") {
    $("#menu1").click();
    } else if($("#queueFilter").val() == "MENU2") {
    $("#menu2").click();
    } else {
    $("#menu3").click();
    }
    }


    Problem: I need to distinguish the simulated startup click() invocation from the real user-initiated one. I thought of passing a custom param somehow, but it's not working. Is it possible to specify a custom handler param, which will be NULL in the other case?



    Ideally what I want is, on startup:



    $("#menu1").click(false); // Indicates a simulated click



    Then in the handler definition:



    $('.menu li').click(customarg, function () { .. }));



    A custom arg would allow me to distinguish in my handler code where I'm coming from and proceed accordingly.










    share|improve this question

























      0












      0








      0







      I have a Click-handler defined which handles clicks on an LI menu item. Suppose the code highlights the active item.



      $('.menu li').click(function () {
      $(this).siblings().removeClass('active');
      $(this).addClass('active');
      // etc.
      }


      On startup, I also simulate a click by calling this handler manually. This is required to highlight an item coming from a server-side URL, which will populate hidden fields. The LI Items have ID's so I manually invoke .click() on them.



      $('document').ready(function () {
      if($("#queueFilter").val() == "MENU1") {
      $("#menu1").click();
      } else if($("#queueFilter").val() == "MENU2") {
      $("#menu2").click();
      } else {
      $("#menu3").click();
      }
      }


      Problem: I need to distinguish the simulated startup click() invocation from the real user-initiated one. I thought of passing a custom param somehow, but it's not working. Is it possible to specify a custom handler param, which will be NULL in the other case?



      Ideally what I want is, on startup:



      $("#menu1").click(false); // Indicates a simulated click



      Then in the handler definition:



      $('.menu li').click(customarg, function () { .. }));



      A custom arg would allow me to distinguish in my handler code where I'm coming from and proceed accordingly.










      share|improve this question













      I have a Click-handler defined which handles clicks on an LI menu item. Suppose the code highlights the active item.



      $('.menu li').click(function () {
      $(this).siblings().removeClass('active');
      $(this).addClass('active');
      // etc.
      }


      On startup, I also simulate a click by calling this handler manually. This is required to highlight an item coming from a server-side URL, which will populate hidden fields. The LI Items have ID's so I manually invoke .click() on them.



      $('document').ready(function () {
      if($("#queueFilter").val() == "MENU1") {
      $("#menu1").click();
      } else if($("#queueFilter").val() == "MENU2") {
      $("#menu2").click();
      } else {
      $("#menu3").click();
      }
      }


      Problem: I need to distinguish the simulated startup click() invocation from the real user-initiated one. I thought of passing a custom param somehow, but it's not working. Is it possible to specify a custom handler param, which will be NULL in the other case?



      Ideally what I want is, on startup:



      $("#menu1").click(false); // Indicates a simulated click



      Then in the handler definition:



      $('.menu li').click(customarg, function () { .. }));



      A custom arg would allow me to distinguish in my handler code where I'm coming from and proceed accordingly.







      jquery






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 27 '18 at 18:12









      gene b.

      1,64852554




      1,64852554
























          3 Answers
          3






          active

          oldest

          votes


















          1














          You can use the .trigger method from jQuery. It allows to send some parameters to the event handler.



          Take a look here: http://api.jquery.com/trigger/



          It would be like this : $('.menu li').trigger('click', { key: value })






          share|improve this answer





























            1














            Which version of jQuery are you using?

            According to the jQuery API documentation you can pass any data you wish to .click() starting from 1.4.3.

            The implementation would look something like this:



            $('document').ready(function () {
            if($("#queueFilter").val() == "MENU1") {
            $("#menu1").click({isSimulated: true}, handleMenuClick);
            } else if($("#queueFilter").val() == "MENU2") {
            $("#menu2").click({isSimulated: true}, handleMenuClick);
            } else {
            $("#menu3").click({isSimulated: true}, handleMenuClick);
            }
            }

            function handleMenuClick(event){
            let clickedItem = $(event.target);
            console.log(clickedItem);

            if (!!event.data && event.data.isSimulated === true) {
            console.log("Simulated click");
            } else {
            console.log("Real click");
            }

            clickedItem.siblings().removeClass('active');
            clickedItem.addClass('active');
            };





            share|improve this answer








            New contributor




            Laurens Deprost is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.


























              0














              I just found the solution: use trigger() and .on() as follows,



              Call the click as



              $("#menu1").trigger('click', false);



              and the handler definition is



              $('.menu li').on('click', function (e, actualUserClick) {
              ...
              });



              In the case where the arg isn't passed (real user click) this arg is undefined.






              share|improve this answer

















              • 1




                Wouldn't you want it to be "fakeUserClick" then? As both false and undefined are falsy, :)
                – Taplar
                Dec 27 '18 at 18:27











              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%2f53949163%2fjquery-pass-custom-parameter-to-common-click-handler%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              You can use the .trigger method from jQuery. It allows to send some parameters to the event handler.



              Take a look here: http://api.jquery.com/trigger/



              It would be like this : $('.menu li').trigger('click', { key: value })






              share|improve this answer


























                1














                You can use the .trigger method from jQuery. It allows to send some parameters to the event handler.



                Take a look here: http://api.jquery.com/trigger/



                It would be like this : $('.menu li').trigger('click', { key: value })






                share|improve this answer
























                  1












                  1








                  1






                  You can use the .trigger method from jQuery. It allows to send some parameters to the event handler.



                  Take a look here: http://api.jquery.com/trigger/



                  It would be like this : $('.menu li').trigger('click', { key: value })






                  share|improve this answer












                  You can use the .trigger method from jQuery. It allows to send some parameters to the event handler.



                  Take a look here: http://api.jquery.com/trigger/



                  It would be like this : $('.menu li').trigger('click', { key: value })







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 27 '18 at 18:22









                  Arthur Almeida

                  29915




                  29915

























                      1














                      Which version of jQuery are you using?

                      According to the jQuery API documentation you can pass any data you wish to .click() starting from 1.4.3.

                      The implementation would look something like this:



                      $('document').ready(function () {
                      if($("#queueFilter").val() == "MENU1") {
                      $("#menu1").click({isSimulated: true}, handleMenuClick);
                      } else if($("#queueFilter").val() == "MENU2") {
                      $("#menu2").click({isSimulated: true}, handleMenuClick);
                      } else {
                      $("#menu3").click({isSimulated: true}, handleMenuClick);
                      }
                      }

                      function handleMenuClick(event){
                      let clickedItem = $(event.target);
                      console.log(clickedItem);

                      if (!!event.data && event.data.isSimulated === true) {
                      console.log("Simulated click");
                      } else {
                      console.log("Real click");
                      }

                      clickedItem.siblings().removeClass('active');
                      clickedItem.addClass('active');
                      };





                      share|improve this answer








                      New contributor




                      Laurens Deprost is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.























                        1














                        Which version of jQuery are you using?

                        According to the jQuery API documentation you can pass any data you wish to .click() starting from 1.4.3.

                        The implementation would look something like this:



                        $('document').ready(function () {
                        if($("#queueFilter").val() == "MENU1") {
                        $("#menu1").click({isSimulated: true}, handleMenuClick);
                        } else if($("#queueFilter").val() == "MENU2") {
                        $("#menu2").click({isSimulated: true}, handleMenuClick);
                        } else {
                        $("#menu3").click({isSimulated: true}, handleMenuClick);
                        }
                        }

                        function handleMenuClick(event){
                        let clickedItem = $(event.target);
                        console.log(clickedItem);

                        if (!!event.data && event.data.isSimulated === true) {
                        console.log("Simulated click");
                        } else {
                        console.log("Real click");
                        }

                        clickedItem.siblings().removeClass('active');
                        clickedItem.addClass('active');
                        };





                        share|improve this answer








                        New contributor




                        Laurens Deprost is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.





















                          1












                          1








                          1






                          Which version of jQuery are you using?

                          According to the jQuery API documentation you can pass any data you wish to .click() starting from 1.4.3.

                          The implementation would look something like this:



                          $('document').ready(function () {
                          if($("#queueFilter").val() == "MENU1") {
                          $("#menu1").click({isSimulated: true}, handleMenuClick);
                          } else if($("#queueFilter").val() == "MENU2") {
                          $("#menu2").click({isSimulated: true}, handleMenuClick);
                          } else {
                          $("#menu3").click({isSimulated: true}, handleMenuClick);
                          }
                          }

                          function handleMenuClick(event){
                          let clickedItem = $(event.target);
                          console.log(clickedItem);

                          if (!!event.data && event.data.isSimulated === true) {
                          console.log("Simulated click");
                          } else {
                          console.log("Real click");
                          }

                          clickedItem.siblings().removeClass('active');
                          clickedItem.addClass('active');
                          };





                          share|improve this answer








                          New contributor




                          Laurens Deprost is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          Which version of jQuery are you using?

                          According to the jQuery API documentation you can pass any data you wish to .click() starting from 1.4.3.

                          The implementation would look something like this:



                          $('document').ready(function () {
                          if($("#queueFilter").val() == "MENU1") {
                          $("#menu1").click({isSimulated: true}, handleMenuClick);
                          } else if($("#queueFilter").val() == "MENU2") {
                          $("#menu2").click({isSimulated: true}, handleMenuClick);
                          } else {
                          $("#menu3").click({isSimulated: true}, handleMenuClick);
                          }
                          }

                          function handleMenuClick(event){
                          let clickedItem = $(event.target);
                          console.log(clickedItem);

                          if (!!event.data && event.data.isSimulated === true) {
                          console.log("Simulated click");
                          } else {
                          console.log("Real click");
                          }

                          clickedItem.siblings().removeClass('active');
                          clickedItem.addClass('active');
                          };






                          share|improve this answer








                          New contributor




                          Laurens Deprost is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          share|improve this answer



                          share|improve this answer






                          New contributor




                          Laurens Deprost is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          answered Dec 27 '18 at 18:25









                          Laurens Deprost

                          87114




                          87114




                          New contributor




                          Laurens Deprost is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.





                          New contributor





                          Laurens Deprost is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.






                          Laurens Deprost is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.























                              0














                              I just found the solution: use trigger() and .on() as follows,



                              Call the click as



                              $("#menu1").trigger('click', false);



                              and the handler definition is



                              $('.menu li').on('click', function (e, actualUserClick) {
                              ...
                              });



                              In the case where the arg isn't passed (real user click) this arg is undefined.






                              share|improve this answer

















                              • 1




                                Wouldn't you want it to be "fakeUserClick" then? As both false and undefined are falsy, :)
                                – Taplar
                                Dec 27 '18 at 18:27
















                              0














                              I just found the solution: use trigger() and .on() as follows,



                              Call the click as



                              $("#menu1").trigger('click', false);



                              and the handler definition is



                              $('.menu li').on('click', function (e, actualUserClick) {
                              ...
                              });



                              In the case where the arg isn't passed (real user click) this arg is undefined.






                              share|improve this answer

















                              • 1




                                Wouldn't you want it to be "fakeUserClick" then? As both false and undefined are falsy, :)
                                – Taplar
                                Dec 27 '18 at 18:27














                              0












                              0








                              0






                              I just found the solution: use trigger() and .on() as follows,



                              Call the click as



                              $("#menu1").trigger('click', false);



                              and the handler definition is



                              $('.menu li').on('click', function (e, actualUserClick) {
                              ...
                              });



                              In the case where the arg isn't passed (real user click) this arg is undefined.






                              share|improve this answer












                              I just found the solution: use trigger() and .on() as follows,



                              Call the click as



                              $("#menu1").trigger('click', false);



                              and the handler definition is



                              $('.menu li').on('click', function (e, actualUserClick) {
                              ...
                              });



                              In the case where the arg isn't passed (real user click) this arg is undefined.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Dec 27 '18 at 18:23









                              gene b.

                              1,64852554




                              1,64852554








                              • 1




                                Wouldn't you want it to be "fakeUserClick" then? As both false and undefined are falsy, :)
                                – Taplar
                                Dec 27 '18 at 18:27














                              • 1




                                Wouldn't you want it to be "fakeUserClick" then? As both false and undefined are falsy, :)
                                – Taplar
                                Dec 27 '18 at 18:27








                              1




                              1




                              Wouldn't you want it to be "fakeUserClick" then? As both false and undefined are falsy, :)
                              – Taplar
                              Dec 27 '18 at 18:27




                              Wouldn't you want it to be "fakeUserClick" then? As both false and undefined are falsy, :)
                              – Taplar
                              Dec 27 '18 at 18:27


















                              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.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • 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%2f53949163%2fjquery-pass-custom-parameter-to-common-click-handler%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

                              Monofisismo

                              Angular Downloading a file using contenturl with Basic Authentication

                              Olmecas