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
javascript jquery xml
migrated from codereview.stackexchange.com Nov 26 at 11:00
This question came from our site for peer programmer code reviews.
add a comment |
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
javascript jquery xml
migrated from codereview.stackexchange.com Nov 26 at 11:00
This question came from our site for peer programmer code reviews.
add a comment |
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
javascript jquery xml
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
javascript jquery xml
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.
add a comment |
add a comment |
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>
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 approachhref:$(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
@JosephZammitthis.href
that is set at the code should be the URL clicked
– guest271314
Nov 26 at 6:05
|
show 1 more comment
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>
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 approachhref:$(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
@JosephZammitthis.href
that is set at the code should be the URL clicked
– guest271314
Nov 26 at 6:05
|
show 1 more comment
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>
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 approachhref:$(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
@JosephZammitthis.href
that is set at the code should be the URL clicked
– guest271314
Nov 26 at 6:05
|
show 1 more comment
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>
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>
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 approachhref:$(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
@JosephZammitthis.href
that is set at the code should be the URL clicked
– guest271314
Nov 26 at 6:05
|
show 1 more comment
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 approachhref:$(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
@JosephZammitthis.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
|
show 1 more comment
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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