Append in parsexml











up vote
0
down vote

favorite












I need some help finishing my code because I'm having a bit of trouble getting the syntax correct. I have a parse XML and I am using append to display the data. I am also trying to do on click event to send data to google analytics but here where I cannot get it right. This is what I want to add onclick="dataLayer.push({'event': 'button1-click'});" in the front end but I cannot add single quote in append.



this is the code



$(document).ready(function () {
$.ajax({
type: "GET",
url: "https://www.nomuraconnects.com/rss/articles",
dataType: "xml",
success: parseXml
});
});

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 target='_blank' href='" + $(this).find("link").text() + "' onclick='dataLayer.push({});'><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>");
});
}


Thanks










share|improve this question













migrated from codereview.stackexchange.com Nov 26 at 11:00


This question came from our site for peer programmer code reviews.



















    up vote
    0
    down vote

    favorite












    I need some help finishing my code because I'm having a bit of trouble getting the syntax correct. I have a parse XML and I am using append to display the data. I am also trying to do on click event to send data to google analytics but here where I cannot get it right. This is what I want to add onclick="dataLayer.push({'event': 'button1-click'});" in the front end but I cannot add single quote in append.



    this is the code



    $(document).ready(function () {
    $.ajax({
    type: "GET",
    url: "https://www.nomuraconnects.com/rss/articles",
    dataType: "xml",
    success: parseXml
    });
    });

    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 target='_blank' href='" + $(this).find("link").text() + "' onclick='dataLayer.push({});'><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>");
    });
    }


    Thanks










    share|improve this question













    migrated from codereview.stackexchange.com Nov 26 at 11:00


    This question came from our site for peer programmer code reviews.

















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I need some help finishing my code because I'm having a bit of trouble getting the syntax correct. I have a parse XML and I am using append to display the data. I am also trying to do on click event to send data to google analytics but here where I cannot get it right. This is what I want to add onclick="dataLayer.push({'event': 'button1-click'});" in the front end but I cannot add single quote in append.



      this is the code



      $(document).ready(function () {
      $.ajax({
      type: "GET",
      url: "https://www.nomuraconnects.com/rss/articles",
      dataType: "xml",
      success: parseXml
      });
      });

      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 target='_blank' href='" + $(this).find("link").text() + "' onclick='dataLayer.push({});'><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>");
      });
      }


      Thanks










      share|improve this question













      I need some help finishing my code because I'm having a bit of trouble getting the syntax correct. I have a parse XML and I am using append to display the data. I am also trying to do on click event to send data to google analytics but here where I cannot get it right. This is what I want to add onclick="dataLayer.push({'event': 'button1-click'});" in the front end but I cannot add single quote in append.



      this is the code



      $(document).ready(function () {
      $.ajax({
      type: "GET",
      url: "https://www.nomuraconnects.com/rss/articles",
      dataType: "xml",
      success: parseXml
      });
      });

      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 target='_blank' href='" + $(this).find("link").text() + "' onclick='dataLayer.push({});'><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>");
      });
      }


      Thanks







      javascript jquery xml






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 26 at 0:59









      Joseph Zammit

      1469




      1469




      migrated from codereview.stackexchange.com Nov 26 at 11:00


      This question came from our site for peer programmer code reviews.






      migrated from codereview.stackexchange.com Nov 26 at 11:00


      This question came from our site for peer programmer code reviews.


























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          You can use jQuery(html, attributes) to set event handlers, HTML and attributes of dynamically created element.






          const dataLayer = ;

          $("#content")
          .append($("<a>",{
          target: "_blank" // set `target` attribute
          , href: "javascript:void 0" // set `href` attribute
          , html: "<div>parent div" // set `innerHTML`
          + "<div>child div 1<img alt='img'/></div>"
          + "<div>child div 2<h2>h2</h2><p>p</p></div></div>"
          , on: { // set event handlers
          click: function(e) { // set `click` event
          dataLayer.push({"event": "button1-click"});
          console.log(dataLayer);
          }
          }
          }));

          .as-console-wrapper{max-height:25% !important}

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <div id="content"></div>








          share|improve this answer





















          • Thanks for the above, all the data is coming from the xml in my previous method I was getting the href by using this +$(this).find("title").text()+ in your method how do I get the data?
            – Joseph Zammit
            Nov 26 at 5:03










          • @JosephZammit Have you tried using the same approach href:$(this).find("title").text()?
            – guest271314
            Nov 26 at 5:09










          • @JosephZammit plnkr.co/edit/lqxm3hA6lqNrBuVAYtF5?p=preview.
            – guest271314
            Nov 26 at 5:40










          • Yes, thanks this worked perfectly. On the click function if I want to collect the url clicked how do i do that?
            – Joseph Zammit
            Nov 26 at 5:58










          • @JosephZammit this.href that is set at the code should be the URL clicked
            – guest271314
            Nov 26 at 6:05











          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%2f53479697%2fappend-in-parsexml%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








          up vote
          0
          down vote



          accepted










          You can use jQuery(html, attributes) to set event handlers, HTML and attributes of dynamically created element.






          const dataLayer = ;

          $("#content")
          .append($("<a>",{
          target: "_blank" // set `target` attribute
          , href: "javascript:void 0" // set `href` attribute
          , html: "<div>parent div" // set `innerHTML`
          + "<div>child div 1<img alt='img'/></div>"
          + "<div>child div 2<h2>h2</h2><p>p</p></div></div>"
          , on: { // set event handlers
          click: function(e) { // set `click` event
          dataLayer.push({"event": "button1-click"});
          console.log(dataLayer);
          }
          }
          }));

          .as-console-wrapper{max-height:25% !important}

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <div id="content"></div>








          share|improve this answer





















          • Thanks for the above, all the data is coming from the xml in my previous method I was getting the href by using this +$(this).find("title").text()+ in your method how do I get the data?
            – Joseph Zammit
            Nov 26 at 5:03










          • @JosephZammit Have you tried using the same approach href:$(this).find("title").text()?
            – guest271314
            Nov 26 at 5:09










          • @JosephZammit plnkr.co/edit/lqxm3hA6lqNrBuVAYtF5?p=preview.
            – guest271314
            Nov 26 at 5:40










          • Yes, thanks this worked perfectly. On the click function if I want to collect the url clicked how do i do that?
            – Joseph Zammit
            Nov 26 at 5:58










          • @JosephZammit this.href that is set at the code should be the URL clicked
            – guest271314
            Nov 26 at 6:05















          up vote
          0
          down vote



          accepted










          You can use jQuery(html, attributes) to set event handlers, HTML and attributes of dynamically created element.






          const dataLayer = ;

          $("#content")
          .append($("<a>",{
          target: "_blank" // set `target` attribute
          , href: "javascript:void 0" // set `href` attribute
          , html: "<div>parent div" // set `innerHTML`
          + "<div>child div 1<img alt='img'/></div>"
          + "<div>child div 2<h2>h2</h2><p>p</p></div></div>"
          , on: { // set event handlers
          click: function(e) { // set `click` event
          dataLayer.push({"event": "button1-click"});
          console.log(dataLayer);
          }
          }
          }));

          .as-console-wrapper{max-height:25% !important}

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <div id="content"></div>








          share|improve this answer





















          • Thanks for the above, all the data is coming from the xml in my previous method I was getting the href by using this +$(this).find("title").text()+ in your method how do I get the data?
            – Joseph Zammit
            Nov 26 at 5:03










          • @JosephZammit Have you tried using the same approach href:$(this).find("title").text()?
            – guest271314
            Nov 26 at 5:09










          • @JosephZammit plnkr.co/edit/lqxm3hA6lqNrBuVAYtF5?p=preview.
            – guest271314
            Nov 26 at 5:40










          • Yes, thanks this worked perfectly. On the click function if I want to collect the url clicked how do i do that?
            – Joseph Zammit
            Nov 26 at 5:58










          • @JosephZammit this.href that is set at the code should be the URL clicked
            – guest271314
            Nov 26 at 6:05













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          You can use jQuery(html, attributes) to set event handlers, HTML and attributes of dynamically created element.






          const dataLayer = ;

          $("#content")
          .append($("<a>",{
          target: "_blank" // set `target` attribute
          , href: "javascript:void 0" // set `href` attribute
          , html: "<div>parent div" // set `innerHTML`
          + "<div>child div 1<img alt='img'/></div>"
          + "<div>child div 2<h2>h2</h2><p>p</p></div></div>"
          , on: { // set event handlers
          click: function(e) { // set `click` event
          dataLayer.push({"event": "button1-click"});
          console.log(dataLayer);
          }
          }
          }));

          .as-console-wrapper{max-height:25% !important}

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <div id="content"></div>








          share|improve this answer












          You can use jQuery(html, attributes) to set event handlers, HTML and attributes of dynamically created element.






          const dataLayer = ;

          $("#content")
          .append($("<a>",{
          target: "_blank" // set `target` attribute
          , href: "javascript:void 0" // set `href` attribute
          , html: "<div>parent div" // set `innerHTML`
          + "<div>child div 1<img alt='img'/></div>"
          + "<div>child div 2<h2>h2</h2><p>p</p></div></div>"
          , on: { // set event handlers
          click: function(e) { // set `click` event
          dataLayer.push({"event": "button1-click"});
          console.log(dataLayer);
          }
          }
          }));

          .as-console-wrapper{max-height:25% !important}

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <div id="content"></div>








          const dataLayer = ;

          $("#content")
          .append($("<a>",{
          target: "_blank" // set `target` attribute
          , href: "javascript:void 0" // set `href` attribute
          , html: "<div>parent div" // set `innerHTML`
          + "<div>child div 1<img alt='img'/></div>"
          + "<div>child div 2<h2>h2</h2><p>p</p></div></div>"
          , on: { // set event handlers
          click: function(e) { // set `click` event
          dataLayer.push({"event": "button1-click"});
          console.log(dataLayer);
          }
          }
          }));

          .as-console-wrapper{max-height:25% !important}

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <div id="content"></div>





          const dataLayer = ;

          $("#content")
          .append($("<a>",{
          target: "_blank" // set `target` attribute
          , href: "javascript:void 0" // set `href` attribute
          , html: "<div>parent div" // set `innerHTML`
          + "<div>child div 1<img alt='img'/></div>"
          + "<div>child div 2<h2>h2</h2><p>p</p></div></div>"
          , on: { // set event handlers
          click: function(e) { // set `click` event
          dataLayer.push({"event": "button1-click"});
          console.log(dataLayer);
          }
          }
          }));

          .as-console-wrapper{max-height:25% !important}

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <div id="content"></div>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 26 at 3:43









          guest271314

          1




          1












          • Thanks for the above, all the data is coming from the xml in my previous method I was getting the href by using this +$(this).find("title").text()+ in your method how do I get the data?
            – Joseph Zammit
            Nov 26 at 5:03










          • @JosephZammit Have you tried using the same approach href:$(this).find("title").text()?
            – guest271314
            Nov 26 at 5:09










          • @JosephZammit plnkr.co/edit/lqxm3hA6lqNrBuVAYtF5?p=preview.
            – guest271314
            Nov 26 at 5:40










          • Yes, thanks this worked perfectly. On the click function if I want to collect the url clicked how do i do that?
            – Joseph Zammit
            Nov 26 at 5:58










          • @JosephZammit this.href that is set at the code should be the URL clicked
            – guest271314
            Nov 26 at 6:05


















          • Thanks for the above, all the data is coming from the xml in my previous method I was getting the href by using this +$(this).find("title").text()+ in your method how do I get the data?
            – Joseph Zammit
            Nov 26 at 5:03










          • @JosephZammit Have you tried using the same approach href:$(this).find("title").text()?
            – guest271314
            Nov 26 at 5:09










          • @JosephZammit plnkr.co/edit/lqxm3hA6lqNrBuVAYtF5?p=preview.
            – guest271314
            Nov 26 at 5:40










          • Yes, thanks this worked perfectly. On the click function if I want to collect the url clicked how do i do that?
            – Joseph Zammit
            Nov 26 at 5:58










          • @JosephZammit this.href that is set at the code should be the URL clicked
            – guest271314
            Nov 26 at 6:05
















          Thanks for the above, all the data is coming from the xml in my previous method I was getting the href by using this +$(this).find("title").text()+ in your method how do I get the data?
          – Joseph Zammit
          Nov 26 at 5:03




          Thanks for the above, all the data is coming from the xml in my previous method I was getting the href by using this +$(this).find("title").text()+ in your method how do I get the data?
          – Joseph Zammit
          Nov 26 at 5:03












          @JosephZammit Have you tried using the same approach href:$(this).find("title").text()?
          – guest271314
          Nov 26 at 5:09




          @JosephZammit Have you tried using the same approach href:$(this).find("title").text()?
          – guest271314
          Nov 26 at 5:09












          @JosephZammit plnkr.co/edit/lqxm3hA6lqNrBuVAYtF5?p=preview.
          – guest271314
          Nov 26 at 5:40




          @JosephZammit plnkr.co/edit/lqxm3hA6lqNrBuVAYtF5?p=preview.
          – guest271314
          Nov 26 at 5:40












          Yes, thanks this worked perfectly. On the click function if I want to collect the url clicked how do i do that?
          – Joseph Zammit
          Nov 26 at 5:58




          Yes, thanks this worked perfectly. On the click function if I want to collect the url clicked how do i do that?
          – Joseph Zammit
          Nov 26 at 5:58












          @JosephZammit this.href that is set at the code should be the URL clicked
          – guest271314
          Nov 26 at 6:05




          @JosephZammit this.href that is set at the code should be the URL clicked
          – guest271314
          Nov 26 at 6:05


















          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%2f53479697%2fappend-in-parsexml%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

          Сан-Квентин

          8-я гвардейская общевойсковая армия

          Алькесар