Multiple Functions on a .on() without impacting scope











up vote
0
down vote

favorite












I am currently making a dynamic webpage and trying to run multiple functions whenever the element with the class=application is clicked. The below code works but it's very repetitive.



$(document).on("click", ".application", renderImage);
$(document).on("click", ".application", renderText);
$(document).on("click", ".application", renderCodeButton);
$(document).on("click", ".application", renderAppButton);


I attempted to put these functions within one master function and run that with the .on() however as I use $(this).attr to pull information nesting the render functions within a master function ruins the scope.



Here is the renderImage function, though they are all very similar:



function renderImage () {
$(".appPicture").empty();
console.log(this);
let applicationPic = $(this).attr("data-image")
let img = $("<img>");
img.addClass("appPic")
img.attr("src", applicationPic);
$(".appPicture").append(img);
}


Is there a way to run multiple functions with a single click event or a way to nest functions within a master without impacting the scope?










share|improve this question




























    up vote
    0
    down vote

    favorite












    I am currently making a dynamic webpage and trying to run multiple functions whenever the element with the class=application is clicked. The below code works but it's very repetitive.



    $(document).on("click", ".application", renderImage);
    $(document).on("click", ".application", renderText);
    $(document).on("click", ".application", renderCodeButton);
    $(document).on("click", ".application", renderAppButton);


    I attempted to put these functions within one master function and run that with the .on() however as I use $(this).attr to pull information nesting the render functions within a master function ruins the scope.



    Here is the renderImage function, though they are all very similar:



    function renderImage () {
    $(".appPicture").empty();
    console.log(this);
    let applicationPic = $(this).attr("data-image")
    let img = $("<img>");
    img.addClass("appPic")
    img.attr("src", applicationPic);
    $(".appPicture").append(img);
    }


    Is there a way to run multiple functions with a single click event or a way to nest functions within a master without impacting the scope?










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am currently making a dynamic webpage and trying to run multiple functions whenever the element with the class=application is clicked. The below code works but it's very repetitive.



      $(document).on("click", ".application", renderImage);
      $(document).on("click", ".application", renderText);
      $(document).on("click", ".application", renderCodeButton);
      $(document).on("click", ".application", renderAppButton);


      I attempted to put these functions within one master function and run that with the .on() however as I use $(this).attr to pull information nesting the render functions within a master function ruins the scope.



      Here is the renderImage function, though they are all very similar:



      function renderImage () {
      $(".appPicture").empty();
      console.log(this);
      let applicationPic = $(this).attr("data-image")
      let img = $("<img>");
      img.addClass("appPic")
      img.attr("src", applicationPic);
      $(".appPicture").append(img);
      }


      Is there a way to run multiple functions with a single click event or a way to nest functions within a master without impacting the scope?










      share|improve this question















      I am currently making a dynamic webpage and trying to run multiple functions whenever the element with the class=application is clicked. The below code works but it's very repetitive.



      $(document).on("click", ".application", renderImage);
      $(document).on("click", ".application", renderText);
      $(document).on("click", ".application", renderCodeButton);
      $(document).on("click", ".application", renderAppButton);


      I attempted to put these functions within one master function and run that with the .on() however as I use $(this).attr to pull information nesting the render functions within a master function ruins the scope.



      Here is the renderImage function, though they are all very similar:



      function renderImage () {
      $(".appPicture").empty();
      console.log(this);
      let applicationPic = $(this).attr("data-image")
      let img = $("<img>");
      img.addClass("appPic")
      img.attr("src", applicationPic);
      $(".appPicture").append(img);
      }


      Is there a way to run multiple functions with a single click event or a way to nest functions within a master without impacting the scope?







      jquery function scope onclick this






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 5 at 4:13









      Shiladitya

      9,29391830




      9,29391830










      asked Nov 5 at 2:47









      A. Andersen

      32




      32
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          $(document).on("click", ".application", function(){
          let scope = this;
          renderImage(scope);
          renderText(scope);
          renderCodeButton(scope);
          renderAppButton(scope);
          });

          function renderImage (scope) {
          $(".appPicture").empty();
          console.log(scope);
          let applicationPic = $(scope).attr("data-image")
          let img = $("<img>");
          img.addClass("appPic")
          img.attr("src", applicationPic);
          $(".appPicture").append(img);
          }


          you can do something like this






          share|improve this answer























          • I've tried that but it changes the scope so $(this) is no longer the item I am clicking but is instead window.
            – A. Andersen
            Nov 5 at 3:05










          • i edit the answer, your function should have args that accept the scope you want..
            – Darryl Ceguerra
            Nov 5 at 3:29










          • Works perfect! Thank you.
            – A. Andersen
            Nov 6 at 0:54











          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',
          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%2f53147681%2fmultiple-functions-on-a-on-without-impacting-scope%23new-answer', 'question_page');
          }
          );

          Post as a guest
































          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          $(document).on("click", ".application", function(){
          let scope = this;
          renderImage(scope);
          renderText(scope);
          renderCodeButton(scope);
          renderAppButton(scope);
          });

          function renderImage (scope) {
          $(".appPicture").empty();
          console.log(scope);
          let applicationPic = $(scope).attr("data-image")
          let img = $("<img>");
          img.addClass("appPic")
          img.attr("src", applicationPic);
          $(".appPicture").append(img);
          }


          you can do something like this






          share|improve this answer























          • I've tried that but it changes the scope so $(this) is no longer the item I am clicking but is instead window.
            – A. Andersen
            Nov 5 at 3:05










          • i edit the answer, your function should have args that accept the scope you want..
            – Darryl Ceguerra
            Nov 5 at 3:29










          • Works perfect! Thank you.
            – A. Andersen
            Nov 6 at 0:54















          up vote
          0
          down vote



          accepted










          $(document).on("click", ".application", function(){
          let scope = this;
          renderImage(scope);
          renderText(scope);
          renderCodeButton(scope);
          renderAppButton(scope);
          });

          function renderImage (scope) {
          $(".appPicture").empty();
          console.log(scope);
          let applicationPic = $(scope).attr("data-image")
          let img = $("<img>");
          img.addClass("appPic")
          img.attr("src", applicationPic);
          $(".appPicture").append(img);
          }


          you can do something like this






          share|improve this answer























          • I've tried that but it changes the scope so $(this) is no longer the item I am clicking but is instead window.
            – A. Andersen
            Nov 5 at 3:05










          • i edit the answer, your function should have args that accept the scope you want..
            – Darryl Ceguerra
            Nov 5 at 3:29










          • Works perfect! Thank you.
            – A. Andersen
            Nov 6 at 0:54













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          $(document).on("click", ".application", function(){
          let scope = this;
          renderImage(scope);
          renderText(scope);
          renderCodeButton(scope);
          renderAppButton(scope);
          });

          function renderImage (scope) {
          $(".appPicture").empty();
          console.log(scope);
          let applicationPic = $(scope).attr("data-image")
          let img = $("<img>");
          img.addClass("appPic")
          img.attr("src", applicationPic);
          $(".appPicture").append(img);
          }


          you can do something like this






          share|improve this answer














          $(document).on("click", ".application", function(){
          let scope = this;
          renderImage(scope);
          renderText(scope);
          renderCodeButton(scope);
          renderAppButton(scope);
          });

          function renderImage (scope) {
          $(".appPicture").empty();
          console.log(scope);
          let applicationPic = $(scope).attr("data-image")
          let img = $("<img>");
          img.addClass("appPic")
          img.attr("src", applicationPic);
          $(".appPicture").append(img);
          }


          you can do something like this







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 5 at 3:27

























          answered Nov 5 at 2:57









          Darryl Ceguerra

          435




          435












          • I've tried that but it changes the scope so $(this) is no longer the item I am clicking but is instead window.
            – A. Andersen
            Nov 5 at 3:05










          • i edit the answer, your function should have args that accept the scope you want..
            – Darryl Ceguerra
            Nov 5 at 3:29










          • Works perfect! Thank you.
            – A. Andersen
            Nov 6 at 0:54


















          • I've tried that but it changes the scope so $(this) is no longer the item I am clicking but is instead window.
            – A. Andersen
            Nov 5 at 3:05










          • i edit the answer, your function should have args that accept the scope you want..
            – Darryl Ceguerra
            Nov 5 at 3:29










          • Works perfect! Thank you.
            – A. Andersen
            Nov 6 at 0:54
















          I've tried that but it changes the scope so $(this) is no longer the item I am clicking but is instead window.
          – A. Andersen
          Nov 5 at 3:05




          I've tried that but it changes the scope so $(this) is no longer the item I am clicking but is instead window.
          – A. Andersen
          Nov 5 at 3:05












          i edit the answer, your function should have args that accept the scope you want..
          – Darryl Ceguerra
          Nov 5 at 3:29




          i edit the answer, your function should have args that accept the scope you want..
          – Darryl Ceguerra
          Nov 5 at 3:29












          Works perfect! Thank you.
          – A. Andersen
          Nov 6 at 0:54




          Works perfect! Thank you.
          – A. Andersen
          Nov 6 at 0:54


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53147681%2fmultiple-functions-on-a-on-without-impacting-scope%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()