find which object called function












0















If I have multiple objects with different IDs but they all call the same function when clicked. Is there any way to track or find which object called the function.



e.g



function test(){
/*function changes background color of 'div' and displays the id of the object that called the function*/

document.getElementById('div').style.backgroundColor = 'red';

var objectID = /*WHATEVER OBJECT CALLED FUNCTION*/;

document.getElementById('textbox').innerHTML = objectID;
}


cheers










share|improve this question


















  • 2





    did you know you can pass arguments to a function? function test(id) then when you call the function like test('someid') then id will be 'someid'

    – Bravo
    Nov 16 '18 at 0:24






  • 1





    Thank you, that will work! That's annoyingly simple haha.

    – Elongated Muskrat
    Nov 16 '18 at 0:26











  • of course, depending on how test is called, it could be simpler, but you haven't shown that very important piece of the puzzle :p

    – Bravo
    Nov 16 '18 at 0:27
















0















If I have multiple objects with different IDs but they all call the same function when clicked. Is there any way to track or find which object called the function.



e.g



function test(){
/*function changes background color of 'div' and displays the id of the object that called the function*/

document.getElementById('div').style.backgroundColor = 'red';

var objectID = /*WHATEVER OBJECT CALLED FUNCTION*/;

document.getElementById('textbox').innerHTML = objectID;
}


cheers










share|improve this question


















  • 2





    did you know you can pass arguments to a function? function test(id) then when you call the function like test('someid') then id will be 'someid'

    – Bravo
    Nov 16 '18 at 0:24






  • 1





    Thank you, that will work! That's annoyingly simple haha.

    – Elongated Muskrat
    Nov 16 '18 at 0:26











  • of course, depending on how test is called, it could be simpler, but you haven't shown that very important piece of the puzzle :p

    – Bravo
    Nov 16 '18 at 0:27














0












0








0








If I have multiple objects with different IDs but they all call the same function when clicked. Is there any way to track or find which object called the function.



e.g



function test(){
/*function changes background color of 'div' and displays the id of the object that called the function*/

document.getElementById('div').style.backgroundColor = 'red';

var objectID = /*WHATEVER OBJECT CALLED FUNCTION*/;

document.getElementById('textbox').innerHTML = objectID;
}


cheers










share|improve this question














If I have multiple objects with different IDs but they all call the same function when clicked. Is there any way to track or find which object called the function.



e.g



function test(){
/*function changes background color of 'div' and displays the id of the object that called the function*/

document.getElementById('div').style.backgroundColor = 'red';

var objectID = /*WHATEVER OBJECT CALLED FUNCTION*/;

document.getElementById('textbox').innerHTML = objectID;
}


cheers







javascript html






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 0:22









Elongated MuskratElongated Muskrat

93




93








  • 2





    did you know you can pass arguments to a function? function test(id) then when you call the function like test('someid') then id will be 'someid'

    – Bravo
    Nov 16 '18 at 0:24






  • 1





    Thank you, that will work! That's annoyingly simple haha.

    – Elongated Muskrat
    Nov 16 '18 at 0:26











  • of course, depending on how test is called, it could be simpler, but you haven't shown that very important piece of the puzzle :p

    – Bravo
    Nov 16 '18 at 0:27














  • 2





    did you know you can pass arguments to a function? function test(id) then when you call the function like test('someid') then id will be 'someid'

    – Bravo
    Nov 16 '18 at 0:24






  • 1





    Thank you, that will work! That's annoyingly simple haha.

    – Elongated Muskrat
    Nov 16 '18 at 0:26











  • of course, depending on how test is called, it could be simpler, but you haven't shown that very important piece of the puzzle :p

    – Bravo
    Nov 16 '18 at 0:27








2




2





did you know you can pass arguments to a function? function test(id) then when you call the function like test('someid') then id will be 'someid'

– Bravo
Nov 16 '18 at 0:24





did you know you can pass arguments to a function? function test(id) then when you call the function like test('someid') then id will be 'someid'

– Bravo
Nov 16 '18 at 0:24




1




1





Thank you, that will work! That's annoyingly simple haha.

– Elongated Muskrat
Nov 16 '18 at 0:26





Thank you, that will work! That's annoyingly simple haha.

– Elongated Muskrat
Nov 16 '18 at 0:26













of course, depending on how test is called, it could be simpler, but you haven't shown that very important piece of the puzzle :p

– Bravo
Nov 16 '18 at 0:27





of course, depending on how test is called, it could be simpler, but you haven't shown that very important piece of the puzzle :p

– Bravo
Nov 16 '18 at 0:27












2 Answers
2






active

oldest

votes


















1














If you're using



onclick="test()"


in the element, change it so that it passes itself to the function:



onclick="test(this)"


Then the function receives the object as the argument.



function test(element) {
var objectID = element.id;
...
}





share|improve this answer































    0














    Just do this:



    function test(id) {

    document.getElementById('div').style.backgroundColor = 'red';

    var objectID = id;

    document.getElementById('textbox').innerHTML = objectID;
    }

    var object = {

    id = "AnObject",
    testFunction: function() {
    test(this.id);
    }

    };

    object.testFunction();





    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%2f53329700%2ffind-which-object-called-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









      1














      If you're using



      onclick="test()"


      in the element, change it so that it passes itself to the function:



      onclick="test(this)"


      Then the function receives the object as the argument.



      function test(element) {
      var objectID = element.id;
      ...
      }





      share|improve this answer




























        1














        If you're using



        onclick="test()"


        in the element, change it so that it passes itself to the function:



        onclick="test(this)"


        Then the function receives the object as the argument.



        function test(element) {
        var objectID = element.id;
        ...
        }





        share|improve this answer


























          1












          1








          1







          If you're using



          onclick="test()"


          in the element, change it so that it passes itself to the function:



          onclick="test(this)"


          Then the function receives the object as the argument.



          function test(element) {
          var objectID = element.id;
          ...
          }





          share|improve this answer













          If you're using



          onclick="test()"


          in the element, change it so that it passes itself to the function:



          onclick="test(this)"


          Then the function receives the object as the argument.



          function test(element) {
          var objectID = element.id;
          ...
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 1:00









          BarmarBarmar

          434k36258359




          434k36258359

























              0














              Just do this:



              function test(id) {

              document.getElementById('div').style.backgroundColor = 'red';

              var objectID = id;

              document.getElementById('textbox').innerHTML = objectID;
              }

              var object = {

              id = "AnObject",
              testFunction: function() {
              test(this.id);
              }

              };

              object.testFunction();





              share|improve this answer




























                0














                Just do this:



                function test(id) {

                document.getElementById('div').style.backgroundColor = 'red';

                var objectID = id;

                document.getElementById('textbox').innerHTML = objectID;
                }

                var object = {

                id = "AnObject",
                testFunction: function() {
                test(this.id);
                }

                };

                object.testFunction();





                share|improve this answer


























                  0












                  0








                  0







                  Just do this:



                  function test(id) {

                  document.getElementById('div').style.backgroundColor = 'red';

                  var objectID = id;

                  document.getElementById('textbox').innerHTML = objectID;
                  }

                  var object = {

                  id = "AnObject",
                  testFunction: function() {
                  test(this.id);
                  }

                  };

                  object.testFunction();





                  share|improve this answer













                  Just do this:



                  function test(id) {

                  document.getElementById('div').style.backgroundColor = 'red';

                  var objectID = id;

                  document.getElementById('textbox').innerHTML = objectID;
                  }

                  var object = {

                  id = "AnObject",
                  testFunction: function() {
                  test(this.id);
                  }

                  };

                  object.testFunction();






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 0:47









                  Jack BashfordJack Bashford

                  13.2k31847




                  13.2k31847






























                      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%2f53329700%2ffind-which-object-called-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

                      Florida Star v. B. J. F.

                      Danny Elfman

                      Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues