Adding onclick() to link in drupal 7











up vote
0
down vote

favorite












I’m working in adding an onclick attribute to a few links on my drupal 7 site. I added the menu_attributes module to assign an id to my links, tried adding the following code to a new js file I included in sites/themes/themeName.info.



function() {
var link = document.getElementById('conversion');
//link.setAttribute("target", "_blank");
link.addEventListener("click", function(e){
return gtag_report_conversion('http://www.mysite/page’); }); }


It’s not adding onclick, would greatly appreciate any help.










share|improve this question




























    up vote
    0
    down vote

    favorite












    I’m working in adding an onclick attribute to a few links on my drupal 7 site. I added the menu_attributes module to assign an id to my links, tried adding the following code to a new js file I included in sites/themes/themeName.info.



    function() {
    var link = document.getElementById('conversion');
    //link.setAttribute("target", "_blank");
    link.addEventListener("click", function(e){
    return gtag_report_conversion('http://www.mysite/page’); }); }


    It’s not adding onclick, would greatly appreciate any help.










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I’m working in adding an onclick attribute to a few links on my drupal 7 site. I added the menu_attributes module to assign an id to my links, tried adding the following code to a new js file I included in sites/themes/themeName.info.



      function() {
      var link = document.getElementById('conversion');
      //link.setAttribute("target", "_blank");
      link.addEventListener("click", function(e){
      return gtag_report_conversion('http://www.mysite/page’); }); }


      It’s not adding onclick, would greatly appreciate any help.










      share|improve this question















      I’m working in adding an onclick attribute to a few links on my drupal 7 site. I added the menu_attributes module to assign an id to my links, tried adding the following code to a new js file I included in sites/themes/themeName.info.



      function() {
      var link = document.getElementById('conversion');
      //link.setAttribute("target", "_blank");
      link.addEventListener("click", function(e){
      return gtag_report_conversion('http://www.mysite/page’); }); }


      It’s not adding onclick, would greatly appreciate any help.







      javascript drupal google-analytics






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 20:33

























      asked Nov 8 at 19:27









      hvannia

      12




      12
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote













          Check whether the script is loading in the page. See this for the details of adding javascript in drupal 7.






          share|improve this answer





















          • I included the 'Add to Head' module to add the other .js scripts. This one I've been trying to test it in developer console. I get the element but the error occurrs when trying to add "onclick()"
            – hvannia
            Nov 9 at 4:04












          • $('#conversation').on('click',function(){ //Code here } Could you try this?
            – Meera
            Nov 9 at 4:08










          • I found this link, I think this is my issue, the script is looking for something not yet existing, stackoverflow.com/questions/14028959/… . Option 1 did not work for me.
            – hvannia
            Nov 10 at 14:59




















          up vote
          0
          down vote













          Based on JavaScript that executes after page load and adjusted for this specific problem this code works if placed inside head



          function addClick(){
          alert('click'); //confirm it works
          return gtag_report_conversion('http://mySite/myPage');
          }

          //make sure this runs after dom is loaded.. so element can be found
          document.addEventListener("DOMContentLoaded", function(){
          var link = document.getElementById("cteconversion");
          link.onclick=addClick;
          });





          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',
            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%2f53214826%2fadding-onclick-to-link-in-drupal-7%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








            up vote
            0
            down vote













            Check whether the script is loading in the page. See this for the details of adding javascript in drupal 7.






            share|improve this answer





















            • I included the 'Add to Head' module to add the other .js scripts. This one I've been trying to test it in developer console. I get the element but the error occurrs when trying to add "onclick()"
              – hvannia
              Nov 9 at 4:04












            • $('#conversation').on('click',function(){ //Code here } Could you try this?
              – Meera
              Nov 9 at 4:08










            • I found this link, I think this is my issue, the script is looking for something not yet existing, stackoverflow.com/questions/14028959/… . Option 1 did not work for me.
              – hvannia
              Nov 10 at 14:59

















            up vote
            0
            down vote













            Check whether the script is loading in the page. See this for the details of adding javascript in drupal 7.






            share|improve this answer





















            • I included the 'Add to Head' module to add the other .js scripts. This one I've been trying to test it in developer console. I get the element but the error occurrs when trying to add "onclick()"
              – hvannia
              Nov 9 at 4:04












            • $('#conversation').on('click',function(){ //Code here } Could you try this?
              – Meera
              Nov 9 at 4:08










            • I found this link, I think this is my issue, the script is looking for something not yet existing, stackoverflow.com/questions/14028959/… . Option 1 did not work for me.
              – hvannia
              Nov 10 at 14:59















            up vote
            0
            down vote










            up vote
            0
            down vote









            Check whether the script is loading in the page. See this for the details of adding javascript in drupal 7.






            share|improve this answer












            Check whether the script is loading in the page. See this for the details of adding javascript in drupal 7.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 9 at 3:22









            Meera

            1026




            1026












            • I included the 'Add to Head' module to add the other .js scripts. This one I've been trying to test it in developer console. I get the element but the error occurrs when trying to add "onclick()"
              – hvannia
              Nov 9 at 4:04












            • $('#conversation').on('click',function(){ //Code here } Could you try this?
              – Meera
              Nov 9 at 4:08










            • I found this link, I think this is my issue, the script is looking for something not yet existing, stackoverflow.com/questions/14028959/… . Option 1 did not work for me.
              – hvannia
              Nov 10 at 14:59




















            • I included the 'Add to Head' module to add the other .js scripts. This one I've been trying to test it in developer console. I get the element but the error occurrs when trying to add "onclick()"
              – hvannia
              Nov 9 at 4:04












            • $('#conversation').on('click',function(){ //Code here } Could you try this?
              – Meera
              Nov 9 at 4:08










            • I found this link, I think this is my issue, the script is looking for something not yet existing, stackoverflow.com/questions/14028959/… . Option 1 did not work for me.
              – hvannia
              Nov 10 at 14:59


















            I included the 'Add to Head' module to add the other .js scripts. This one I've been trying to test it in developer console. I get the element but the error occurrs when trying to add "onclick()"
            – hvannia
            Nov 9 at 4:04






            I included the 'Add to Head' module to add the other .js scripts. This one I've been trying to test it in developer console. I get the element but the error occurrs when trying to add "onclick()"
            – hvannia
            Nov 9 at 4:04














            $('#conversation').on('click',function(){ //Code here } Could you try this?
            – Meera
            Nov 9 at 4:08




            $('#conversation').on('click',function(){ //Code here } Could you try this?
            – Meera
            Nov 9 at 4:08












            I found this link, I think this is my issue, the script is looking for something not yet existing, stackoverflow.com/questions/14028959/… . Option 1 did not work for me.
            – hvannia
            Nov 10 at 14:59






            I found this link, I think this is my issue, the script is looking for something not yet existing, stackoverflow.com/questions/14028959/… . Option 1 did not work for me.
            – hvannia
            Nov 10 at 14:59














            up vote
            0
            down vote













            Based on JavaScript that executes after page load and adjusted for this specific problem this code works if placed inside head



            function addClick(){
            alert('click'); //confirm it works
            return gtag_report_conversion('http://mySite/myPage');
            }

            //make sure this runs after dom is loaded.. so element can be found
            document.addEventListener("DOMContentLoaded", function(){
            var link = document.getElementById("cteconversion");
            link.onclick=addClick;
            });





            share|improve this answer

























              up vote
              0
              down vote













              Based on JavaScript that executes after page load and adjusted for this specific problem this code works if placed inside head



              function addClick(){
              alert('click'); //confirm it works
              return gtag_report_conversion('http://mySite/myPage');
              }

              //make sure this runs after dom is loaded.. so element can be found
              document.addEventListener("DOMContentLoaded", function(){
              var link = document.getElementById("cteconversion");
              link.onclick=addClick;
              });





              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                Based on JavaScript that executes after page load and adjusted for this specific problem this code works if placed inside head



                function addClick(){
                alert('click'); //confirm it works
                return gtag_report_conversion('http://mySite/myPage');
                }

                //make sure this runs after dom is loaded.. so element can be found
                document.addEventListener("DOMContentLoaded", function(){
                var link = document.getElementById("cteconversion");
                link.onclick=addClick;
                });





                share|improve this answer












                Based on JavaScript that executes after page load and adjusted for this specific problem this code works if placed inside head



                function addClick(){
                alert('click'); //confirm it works
                return gtag_report_conversion('http://mySite/myPage');
                }

                //make sure this runs after dom is loaded.. so element can be found
                document.addEventListener("DOMContentLoaded", function(){
                var link = document.getElementById("cteconversion");
                link.onclick=addClick;
                });






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 12 at 5:17









                hvannia

                12




                12






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53214826%2fadding-onclick-to-link-in-drupal-7%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

                    Schultheiß

                    Verwaltungsgliederung Dänemarks

                    Liste der Kulturdenkmale in Wilsdruff