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 yesterday
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 yesterday
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 2 days ago
Joseph Zammit
1469
1469
migrated from codereview.stackexchange.com yesterday
This question came from our site for peer programmer code reviews.
migrated from codereview.stackexchange.com yesterday
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
yesterday
@JosephZammit Have you tried using the same approachhref:$(this).find("title").text()
?
– guest271314
yesterday
@JosephZammit plnkr.co/edit/lqxm3hA6lqNrBuVAYtF5?p=preview.
– guest271314
yesterday
Yes, thanks this worked perfectly. On the click function if I want to collect the url clicked how do i do that?
– Joseph Zammit
yesterday
@JosephZammitthis.href
that is set at the code should be the URL clicked
– guest271314
yesterday
|
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
yesterday
@JosephZammit Have you tried using the same approachhref:$(this).find("title").text()
?
– guest271314
yesterday
@JosephZammit plnkr.co/edit/lqxm3hA6lqNrBuVAYtF5?p=preview.
– guest271314
yesterday
Yes, thanks this worked perfectly. On the click function if I want to collect the url clicked how do i do that?
– Joseph Zammit
yesterday
@JosephZammitthis.href
that is set at the code should be the URL clicked
– guest271314
yesterday
|
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
yesterday
@JosephZammit Have you tried using the same approachhref:$(this).find("title").text()
?
– guest271314
yesterday
@JosephZammit plnkr.co/edit/lqxm3hA6lqNrBuVAYtF5?p=preview.
– guest271314
yesterday
Yes, thanks this worked perfectly. On the click function if I want to collect the url clicked how do i do that?
– Joseph Zammit
yesterday
@JosephZammitthis.href
that is set at the code should be the URL clicked
– guest271314
yesterday
|
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 2 days ago
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
yesterday
@JosephZammit Have you tried using the same approachhref:$(this).find("title").text()
?
– guest271314
yesterday
@JosephZammit plnkr.co/edit/lqxm3hA6lqNrBuVAYtF5?p=preview.
– guest271314
yesterday
Yes, thanks this worked perfectly. On the click function if I want to collect the url clicked how do i do that?
– Joseph Zammit
yesterday
@JosephZammitthis.href
that is set at the code should be the URL clicked
– guest271314
yesterday
|
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
yesterday
@JosephZammit Have you tried using the same approachhref:$(this).find("title").text()
?
– guest271314
yesterday
@JosephZammit plnkr.co/edit/lqxm3hA6lqNrBuVAYtF5?p=preview.
– guest271314
yesterday
Yes, thanks this worked perfectly. On the click function if I want to collect the url clicked how do i do that?
– Joseph Zammit
yesterday
@JosephZammitthis.href
that is set at the code should be the URL clicked
– guest271314
yesterday
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
yesterday
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
yesterday
@JosephZammit Have you tried using the same approach
href:$(this).find("title").text()
?– guest271314
yesterday
@JosephZammit Have you tried using the same approach
href:$(this).find("title").text()
?– guest271314
yesterday
@JosephZammit plnkr.co/edit/lqxm3hA6lqNrBuVAYtF5?p=preview.
– guest271314
yesterday
@JosephZammit plnkr.co/edit/lqxm3hA6lqNrBuVAYtF5?p=preview.
– guest271314
yesterday
Yes, thanks this worked perfectly. On the click function if I want to collect the url clicked how do i do that?
– Joseph Zammit
yesterday
Yes, thanks this worked perfectly. On the click function if I want to collect the url clicked how do i do that?
– Joseph Zammit
yesterday
@JosephZammit
this.href
that is set at the code should be the URL clicked– guest271314
yesterday
@JosephZammit
this.href
that is set at the code should be the URL clicked– guest271314
yesterday
|
show 1 more comment
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