on click event in append












1














I'm trying to add on click event in a parsexml append but having issues getting it done correctly keeps getting console errors. Maybe I'm doing something wrong in the code but not sure how I should do it. Basically I'm trying to get click events in google analytics using datalayer. this is the code



function parseXml(xml)
{
$("#main").html("<div class='section group' id='content' data-
role='listview' data-inset='true'></div>");
$(xml).find("item").slice(0, 2).each(function()
{
$("#content").append("<a onclick="
dataLayer.push({
'event': 'myTrackEvent',
'eventCategory': 'clicked',
'eventAction': 'click',
'eventLabel': 'article click'
});"class='articleClick' target='_blank'
href='"+$(this).find("link").text()+"'><div class='col span_1_of_2'>
<div class='thumbnail'><img
src='"+$(this).find("enclosure").attr('url')
+"'/></div><div class='text-box'><h2>"+$(this).find("title").text()+"
</h2><p>"+$(this).find("description").text()+"</p></div></div>
</a>");
});

}









share|improve this question



























    1














    I'm trying to add on click event in a parsexml append but having issues getting it done correctly keeps getting console errors. Maybe I'm doing something wrong in the code but not sure how I should do it. Basically I'm trying to get click events in google analytics using datalayer. this is the code



    function parseXml(xml)
    {
    $("#main").html("<div class='section group' id='content' data-
    role='listview' data-inset='true'></div>");
    $(xml).find("item").slice(0, 2).each(function()
    {
    $("#content").append("<a onclick="
    dataLayer.push({
    'event': 'myTrackEvent',
    'eventCategory': 'clicked',
    'eventAction': 'click',
    'eventLabel': 'article click'
    });"class='articleClick' target='_blank'
    href='"+$(this).find("link").text()+"'><div class='col span_1_of_2'>
    <div class='thumbnail'><img
    src='"+$(this).find("enclosure").attr('url')
    +"'/></div><div class='text-box'><h2>"+$(this).find("title").text()+"
    </h2><p>"+$(this).find("description").text()+"</p></div></div>
    </a>");
    });

    }









    share|improve this question

























      1












      1








      1







      I'm trying to add on click event in a parsexml append but having issues getting it done correctly keeps getting console errors. Maybe I'm doing something wrong in the code but not sure how I should do it. Basically I'm trying to get click events in google analytics using datalayer. this is the code



      function parseXml(xml)
      {
      $("#main").html("<div class='section group' id='content' data-
      role='listview' data-inset='true'></div>");
      $(xml).find("item").slice(0, 2).each(function()
      {
      $("#content").append("<a onclick="
      dataLayer.push({
      'event': 'myTrackEvent',
      'eventCategory': 'clicked',
      'eventAction': 'click',
      'eventLabel': 'article click'
      });"class='articleClick' target='_blank'
      href='"+$(this).find("link").text()+"'><div class='col span_1_of_2'>
      <div class='thumbnail'><img
      src='"+$(this).find("enclosure").attr('url')
      +"'/></div><div class='text-box'><h2>"+$(this).find("title").text()+"
      </h2><p>"+$(this).find("description").text()+"</p></div></div>
      </a>");
      });

      }









      share|improve this question













      I'm trying to add on click event in a parsexml append but having issues getting it done correctly keeps getting console errors. Maybe I'm doing something wrong in the code but not sure how I should do it. Basically I'm trying to get click events in google analytics using datalayer. this is the code



      function parseXml(xml)
      {
      $("#main").html("<div class='section group' id='content' data-
      role='listview' data-inset='true'></div>");
      $(xml).find("item").slice(0, 2).each(function()
      {
      $("#content").append("<a onclick="
      dataLayer.push({
      'event': 'myTrackEvent',
      'eventCategory': 'clicked',
      'eventAction': 'click',
      'eventLabel': 'article click'
      });"class='articleClick' target='_blank'
      href='"+$(this).find("link").text()+"'><div class='col span_1_of_2'>
      <div class='thumbnail'><img
      src='"+$(this).find("enclosure").attr('url')
      +"'/></div><div class='text-box'><h2>"+$(this).find("title").text()+"
      </h2><p>"+$(this).find("description").text()+"</p></div></div>
      </a>");
      });

      }






      javascript jquery google-analytics






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 7:25









      Joseph Zammit

      1469




      1469
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Your code is invalid here:



          $("#content").append("<a onclick="
          dataLayer.push({


          .append() expects you to finish the string



          $("#content").append("<a onclick="  dataLayer.push({..    // that is what javascript sees


          what you need to do is append the string:



          $("#content").append("<a onclick=" +
          dataLayer.push({


          or



          $("#content").append("<a onclick=")
          .append(dataLayer.push({...}));





          share|improve this answer





















          • so it becomes like this? $("#content").append("<a onclick="+dataLayer.push({ 'event': 'myTrackEvent','eventCategory': 'clicked','eventAction': 'click','eventLabel': 'article click' }) + " target='_blank' href='"+$(this).find("link").text()+"'><div class='col span_1_of_2'><div class='thumbnail'><img src='"+$(this).find("enclosure").attr('url') +"'/></div><div class='text-box'><h2>"+$(this).find("title").text()+"</h2><p>"+$(this).find("description").text()+"</p></div></div></a>");
            – Joseph Zammit
            Nov 21 '18 at 7:49












          • You need to make sure that whatever you append to the string, it should output a valid HTML syntax. It should complete the HTML tag <a onclick="some_function()" class="some_class">some_text</a>
            – Ahmad
            Nov 21 '18 at 7:51










          • In my answer, I assumed that dataLayer.push({..etc..}) will complete the HTML tag for the link. And to do that, you need to properly append to the string started the line before. @JosephZammit
            – Ahmad
            Nov 21 '18 at 7:53










          • Not sure if i understand what you are saying the code looks like it is complete with the closing tags but for some reason it is not working. is that the correct method to do this or there is another method?
            – Joseph Zammit
            Nov 21 '18 at 8:05










          • First, you need to make sure what value is returned by dataLayer.push() function. Does it return string? if yes, then you need to properly attach the return value with the <a onclick=". If not, then you need to fix your code to complete the HTML link tag @JosephZammit
            – Ahmad
            Nov 21 '18 at 8:07













          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%2f53407095%2fon-click-event-in-append%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Your code is invalid here:



          $("#content").append("<a onclick="
          dataLayer.push({


          .append() expects you to finish the string



          $("#content").append("<a onclick="  dataLayer.push({..    // that is what javascript sees


          what you need to do is append the string:



          $("#content").append("<a onclick=" +
          dataLayer.push({


          or



          $("#content").append("<a onclick=")
          .append(dataLayer.push({...}));





          share|improve this answer





















          • so it becomes like this? $("#content").append("<a onclick="+dataLayer.push({ 'event': 'myTrackEvent','eventCategory': 'clicked','eventAction': 'click','eventLabel': 'article click' }) + " target='_blank' href='"+$(this).find("link").text()+"'><div class='col span_1_of_2'><div class='thumbnail'><img src='"+$(this).find("enclosure").attr('url') +"'/></div><div class='text-box'><h2>"+$(this).find("title").text()+"</h2><p>"+$(this).find("description").text()+"</p></div></div></a>");
            – Joseph Zammit
            Nov 21 '18 at 7:49












          • You need to make sure that whatever you append to the string, it should output a valid HTML syntax. It should complete the HTML tag <a onclick="some_function()" class="some_class">some_text</a>
            – Ahmad
            Nov 21 '18 at 7:51










          • In my answer, I assumed that dataLayer.push({..etc..}) will complete the HTML tag for the link. And to do that, you need to properly append to the string started the line before. @JosephZammit
            – Ahmad
            Nov 21 '18 at 7:53










          • Not sure if i understand what you are saying the code looks like it is complete with the closing tags but for some reason it is not working. is that the correct method to do this or there is another method?
            – Joseph Zammit
            Nov 21 '18 at 8:05










          • First, you need to make sure what value is returned by dataLayer.push() function. Does it return string? if yes, then you need to properly attach the return value with the <a onclick=". If not, then you need to fix your code to complete the HTML link tag @JosephZammit
            – Ahmad
            Nov 21 '18 at 8:07


















          0














          Your code is invalid here:



          $("#content").append("<a onclick="
          dataLayer.push({


          .append() expects you to finish the string



          $("#content").append("<a onclick="  dataLayer.push({..    // that is what javascript sees


          what you need to do is append the string:



          $("#content").append("<a onclick=" +
          dataLayer.push({


          or



          $("#content").append("<a onclick=")
          .append(dataLayer.push({...}));





          share|improve this answer





















          • so it becomes like this? $("#content").append("<a onclick="+dataLayer.push({ 'event': 'myTrackEvent','eventCategory': 'clicked','eventAction': 'click','eventLabel': 'article click' }) + " target='_blank' href='"+$(this).find("link").text()+"'><div class='col span_1_of_2'><div class='thumbnail'><img src='"+$(this).find("enclosure").attr('url') +"'/></div><div class='text-box'><h2>"+$(this).find("title").text()+"</h2><p>"+$(this).find("description").text()+"</p></div></div></a>");
            – Joseph Zammit
            Nov 21 '18 at 7:49












          • You need to make sure that whatever you append to the string, it should output a valid HTML syntax. It should complete the HTML tag <a onclick="some_function()" class="some_class">some_text</a>
            – Ahmad
            Nov 21 '18 at 7:51










          • In my answer, I assumed that dataLayer.push({..etc..}) will complete the HTML tag for the link. And to do that, you need to properly append to the string started the line before. @JosephZammit
            – Ahmad
            Nov 21 '18 at 7:53










          • Not sure if i understand what you are saying the code looks like it is complete with the closing tags but for some reason it is not working. is that the correct method to do this or there is another method?
            – Joseph Zammit
            Nov 21 '18 at 8:05










          • First, you need to make sure what value is returned by dataLayer.push() function. Does it return string? if yes, then you need to properly attach the return value with the <a onclick=". If not, then you need to fix your code to complete the HTML link tag @JosephZammit
            – Ahmad
            Nov 21 '18 at 8:07
















          0












          0








          0






          Your code is invalid here:



          $("#content").append("<a onclick="
          dataLayer.push({


          .append() expects you to finish the string



          $("#content").append("<a onclick="  dataLayer.push({..    // that is what javascript sees


          what you need to do is append the string:



          $("#content").append("<a onclick=" +
          dataLayer.push({


          or



          $("#content").append("<a onclick=")
          .append(dataLayer.push({...}));





          share|improve this answer












          Your code is invalid here:



          $("#content").append("<a onclick="
          dataLayer.push({


          .append() expects you to finish the string



          $("#content").append("<a onclick="  dataLayer.push({..    // that is what javascript sees


          what you need to do is append the string:



          $("#content").append("<a onclick=" +
          dataLayer.push({


          or



          $("#content").append("<a onclick=")
          .append(dataLayer.push({...}));






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 7:39









          Ahmad

          8,20743463




          8,20743463












          • so it becomes like this? $("#content").append("<a onclick="+dataLayer.push({ 'event': 'myTrackEvent','eventCategory': 'clicked','eventAction': 'click','eventLabel': 'article click' }) + " target='_blank' href='"+$(this).find("link").text()+"'><div class='col span_1_of_2'><div class='thumbnail'><img src='"+$(this).find("enclosure").attr('url') +"'/></div><div class='text-box'><h2>"+$(this).find("title").text()+"</h2><p>"+$(this).find("description").text()+"</p></div></div></a>");
            – Joseph Zammit
            Nov 21 '18 at 7:49












          • You need to make sure that whatever you append to the string, it should output a valid HTML syntax. It should complete the HTML tag <a onclick="some_function()" class="some_class">some_text</a>
            – Ahmad
            Nov 21 '18 at 7:51










          • In my answer, I assumed that dataLayer.push({..etc..}) will complete the HTML tag for the link. And to do that, you need to properly append to the string started the line before. @JosephZammit
            – Ahmad
            Nov 21 '18 at 7:53










          • Not sure if i understand what you are saying the code looks like it is complete with the closing tags but for some reason it is not working. is that the correct method to do this or there is another method?
            – Joseph Zammit
            Nov 21 '18 at 8:05










          • First, you need to make sure what value is returned by dataLayer.push() function. Does it return string? if yes, then you need to properly attach the return value with the <a onclick=". If not, then you need to fix your code to complete the HTML link tag @JosephZammit
            – Ahmad
            Nov 21 '18 at 8:07




















          • so it becomes like this? $("#content").append("<a onclick="+dataLayer.push({ 'event': 'myTrackEvent','eventCategory': 'clicked','eventAction': 'click','eventLabel': 'article click' }) + " target='_blank' href='"+$(this).find("link").text()+"'><div class='col span_1_of_2'><div class='thumbnail'><img src='"+$(this).find("enclosure").attr('url') +"'/></div><div class='text-box'><h2>"+$(this).find("title").text()+"</h2><p>"+$(this).find("description").text()+"</p></div></div></a>");
            – Joseph Zammit
            Nov 21 '18 at 7:49












          • You need to make sure that whatever you append to the string, it should output a valid HTML syntax. It should complete the HTML tag <a onclick="some_function()" class="some_class">some_text</a>
            – Ahmad
            Nov 21 '18 at 7:51










          • In my answer, I assumed that dataLayer.push({..etc..}) will complete the HTML tag for the link. And to do that, you need to properly append to the string started the line before. @JosephZammit
            – Ahmad
            Nov 21 '18 at 7:53










          • Not sure if i understand what you are saying the code looks like it is complete with the closing tags but for some reason it is not working. is that the correct method to do this or there is another method?
            – Joseph Zammit
            Nov 21 '18 at 8:05










          • First, you need to make sure what value is returned by dataLayer.push() function. Does it return string? if yes, then you need to properly attach the return value with the <a onclick=". If not, then you need to fix your code to complete the HTML link tag @JosephZammit
            – Ahmad
            Nov 21 '18 at 8:07


















          so it becomes like this? $("#content").append("<a onclick="+dataLayer.push({ 'event': 'myTrackEvent','eventCategory': 'clicked','eventAction': 'click','eventLabel': 'article click' }) + " target='_blank' href='"+$(this).find("link").text()+"'><div class='col span_1_of_2'><div class='thumbnail'><img src='"+$(this).find("enclosure").attr('url') +"'/></div><div class='text-box'><h2>"+$(this).find("title").text()+"</h2><p>"+$(this).find("description").text()+"</p></div></div></a>");
          – Joseph Zammit
          Nov 21 '18 at 7:49






          so it becomes like this? $("#content").append("<a onclick="+dataLayer.push({ 'event': 'myTrackEvent','eventCategory': 'clicked','eventAction': 'click','eventLabel': 'article click' }) + " target='_blank' href='"+$(this).find("link").text()+"'><div class='col span_1_of_2'><div class='thumbnail'><img src='"+$(this).find("enclosure").attr('url') +"'/></div><div class='text-box'><h2>"+$(this).find("title").text()+"</h2><p>"+$(this).find("description").text()+"</p></div></div></a>");
          – Joseph Zammit
          Nov 21 '18 at 7:49














          You need to make sure that whatever you append to the string, it should output a valid HTML syntax. It should complete the HTML tag <a onclick="some_function()" class="some_class">some_text</a>
          – Ahmad
          Nov 21 '18 at 7:51




          You need to make sure that whatever you append to the string, it should output a valid HTML syntax. It should complete the HTML tag <a onclick="some_function()" class="some_class">some_text</a>
          – Ahmad
          Nov 21 '18 at 7:51












          In my answer, I assumed that dataLayer.push({..etc..}) will complete the HTML tag for the link. And to do that, you need to properly append to the string started the line before. @JosephZammit
          – Ahmad
          Nov 21 '18 at 7:53




          In my answer, I assumed that dataLayer.push({..etc..}) will complete the HTML tag for the link. And to do that, you need to properly append to the string started the line before. @JosephZammit
          – Ahmad
          Nov 21 '18 at 7:53












          Not sure if i understand what you are saying the code looks like it is complete with the closing tags but for some reason it is not working. is that the correct method to do this or there is another method?
          – Joseph Zammit
          Nov 21 '18 at 8:05




          Not sure if i understand what you are saying the code looks like it is complete with the closing tags but for some reason it is not working. is that the correct method to do this or there is another method?
          – Joseph Zammit
          Nov 21 '18 at 8:05












          First, you need to make sure what value is returned by dataLayer.push() function. Does it return string? if yes, then you need to properly attach the return value with the <a onclick=". If not, then you need to fix your code to complete the HTML link tag @JosephZammit
          – Ahmad
          Nov 21 '18 at 8:07






          First, you need to make sure what value is returned by dataLayer.push() function. Does it return string? if yes, then you need to properly attach the return value with the <a onclick=". If not, then you need to fix your code to complete the HTML link tag @JosephZammit
          – Ahmad
          Nov 21 '18 at 8:07




















          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%2f53407095%2fon-click-event-in-append%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

          Wiesbaden

          Marschland

          Dieringhausen