How to parse select second HTML anchor element within a JSON response
I have a JSON response which is returning correctly and have successfully inputted data from it on my HTML page. However, this one JSON value returns some HTML code in which I do not need all of it, just the second anchor element. How would you guys do this?
The code below, the top two work fine. The P element does not. Any ideas? I just get undefined
in the console.
// get photo url from description JSON value.
var imageURL = $(description).find('a')[ 1 ];
var parseImageURL = $(imageURL).attr('href');
// get author URL from description JSON value
var authorURL = $(description).find('a')[ 0 ];
var parseAuthorURL = $(authorURL).attr('href');
// get photo description from descriptions JSON value
var descriptionText = $(description).find('P')[5];
console.log(descriptionText);
javascript jquery html
add a comment |
I have a JSON response which is returning correctly and have successfully inputted data from it on my HTML page. However, this one JSON value returns some HTML code in which I do not need all of it, just the second anchor element. How would you guys do this?
The code below, the top two work fine. The P element does not. Any ideas? I just get undefined
in the console.
// get photo url from description JSON value.
var imageURL = $(description).find('a')[ 1 ];
var parseImageURL = $(imageURL).attr('href');
// get author URL from description JSON value
var authorURL = $(description).find('a')[ 0 ];
var parseAuthorURL = $(authorURL).attr('href');
// get photo description from descriptions JSON value
var descriptionText = $(description).find('P')[5];
console.log(descriptionText);
javascript jquery html
Two things that could have happened: 1) you have got the indexes mixed up (likely since there are lots ofp
elements) 2) You've got some undefined variables (what isdescription
?)
– Jack Bashford
Nov 16 '18 at 0:54
Hey Jack. That's the thing, description is pretty deep inside the JSON so there's hardly any tags. it is too long to post, but there is only 3 <p> elements and 2 'text' elemetns
– ConwayStern
Nov 16 '18 at 0:59
add a comment |
I have a JSON response which is returning correctly and have successfully inputted data from it on my HTML page. However, this one JSON value returns some HTML code in which I do not need all of it, just the second anchor element. How would you guys do this?
The code below, the top two work fine. The P element does not. Any ideas? I just get undefined
in the console.
// get photo url from description JSON value.
var imageURL = $(description).find('a')[ 1 ];
var parseImageURL = $(imageURL).attr('href');
// get author URL from description JSON value
var authorURL = $(description).find('a')[ 0 ];
var parseAuthorURL = $(authorURL).attr('href');
// get photo description from descriptions JSON value
var descriptionText = $(description).find('P')[5];
console.log(descriptionText);
javascript jquery html
I have a JSON response which is returning correctly and have successfully inputted data from it on my HTML page. However, this one JSON value returns some HTML code in which I do not need all of it, just the second anchor element. How would you guys do this?
The code below, the top two work fine. The P element does not. Any ideas? I just get undefined
in the console.
// get photo url from description JSON value.
var imageURL = $(description).find('a')[ 1 ];
var parseImageURL = $(imageURL).attr('href');
// get author URL from description JSON value
var authorURL = $(description).find('a')[ 0 ];
var parseAuthorURL = $(authorURL).attr('href');
// get photo description from descriptions JSON value
var descriptionText = $(description).find('P')[5];
console.log(descriptionText);
javascript jquery html
javascript jquery html
asked Nov 16 '18 at 0:49
ConwaySternConwayStern
175
175
Two things that could have happened: 1) you have got the indexes mixed up (likely since there are lots ofp
elements) 2) You've got some undefined variables (what isdescription
?)
– Jack Bashford
Nov 16 '18 at 0:54
Hey Jack. That's the thing, description is pretty deep inside the JSON so there's hardly any tags. it is too long to post, but there is only 3 <p> elements and 2 'text' elemetns
– ConwayStern
Nov 16 '18 at 0:59
add a comment |
Two things that could have happened: 1) you have got the indexes mixed up (likely since there are lots ofp
elements) 2) You've got some undefined variables (what isdescription
?)
– Jack Bashford
Nov 16 '18 at 0:54
Hey Jack. That's the thing, description is pretty deep inside the JSON so there's hardly any tags. it is too long to post, but there is only 3 <p> elements and 2 'text' elemetns
– ConwayStern
Nov 16 '18 at 0:59
Two things that could have happened: 1) you have got the indexes mixed up (likely since there are lots of
p
elements) 2) You've got some undefined variables (what is description
?)– Jack Bashford
Nov 16 '18 at 0:54
Two things that could have happened: 1) you have got the indexes mixed up (likely since there are lots of
p
elements) 2) You've got some undefined variables (what is description
?)– Jack Bashford
Nov 16 '18 at 0:54
Hey Jack. That's the thing, description is pretty deep inside the JSON so there's hardly any tags. it is too long to post, but there is only 3 <p> elements and 2 'text' elemetns
– ConwayStern
Nov 16 '18 at 0:59
Hey Jack. That's the thing, description is pretty deep inside the JSON so there's hardly any tags. it is too long to post, but there is only 3 <p> elements and 2 'text' elemetns
– ConwayStern
Nov 16 '18 at 0:59
add a comment |
1 Answer
1
active
oldest
votes
Parse HTML
You need to "parse" the HTML. There's (at least) two ways to do it:
DOMParser or create an element and stuff the HTML string in there.
The following example shows the second method.
- In my example, I assign some string to
HTML
but you are getting it in some object that you have parsed from JSON. - Create an element. Could be any old element, but I chose
div
- assign the HTML string to the
innerHTML
of the element created in step 2 - use querySelectorAll to select the node you want. In this example, I selected all of the
p
elements. - I simply logged out the second
p
, but in your case, you add it to the page, I assume
let HTML = "<p>something</p><p>else</p>";
let el = document.createElement('div');
el.innerHTML = HTML;
let ps = el.querySelectorAll('p');
console.log(ps[1]);
That worked!! Thank you Will
– ConwayStern
Nov 16 '18 at 10:43
add a comment |
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
);
);
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%2f53329901%2fhow-to-parse-select-second-html-anchor-element-within-a-json-response%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
Parse HTML
You need to "parse" the HTML. There's (at least) two ways to do it:
DOMParser or create an element and stuff the HTML string in there.
The following example shows the second method.
- In my example, I assign some string to
HTML
but you are getting it in some object that you have parsed from JSON. - Create an element. Could be any old element, but I chose
div
- assign the HTML string to the
innerHTML
of the element created in step 2 - use querySelectorAll to select the node you want. In this example, I selected all of the
p
elements. - I simply logged out the second
p
, but in your case, you add it to the page, I assume
let HTML = "<p>something</p><p>else</p>";
let el = document.createElement('div');
el.innerHTML = HTML;
let ps = el.querySelectorAll('p');
console.log(ps[1]);
That worked!! Thank you Will
– ConwayStern
Nov 16 '18 at 10:43
add a comment |
Parse HTML
You need to "parse" the HTML. There's (at least) two ways to do it:
DOMParser or create an element and stuff the HTML string in there.
The following example shows the second method.
- In my example, I assign some string to
HTML
but you are getting it in some object that you have parsed from JSON. - Create an element. Could be any old element, but I chose
div
- assign the HTML string to the
innerHTML
of the element created in step 2 - use querySelectorAll to select the node you want. In this example, I selected all of the
p
elements. - I simply logged out the second
p
, but in your case, you add it to the page, I assume
let HTML = "<p>something</p><p>else</p>";
let el = document.createElement('div');
el.innerHTML = HTML;
let ps = el.querySelectorAll('p');
console.log(ps[1]);
That worked!! Thank you Will
– ConwayStern
Nov 16 '18 at 10:43
add a comment |
Parse HTML
You need to "parse" the HTML. There's (at least) two ways to do it:
DOMParser or create an element and stuff the HTML string in there.
The following example shows the second method.
- In my example, I assign some string to
HTML
but you are getting it in some object that you have parsed from JSON. - Create an element. Could be any old element, but I chose
div
- assign the HTML string to the
innerHTML
of the element created in step 2 - use querySelectorAll to select the node you want. In this example, I selected all of the
p
elements. - I simply logged out the second
p
, but in your case, you add it to the page, I assume
let HTML = "<p>something</p><p>else</p>";
let el = document.createElement('div');
el.innerHTML = HTML;
let ps = el.querySelectorAll('p');
console.log(ps[1]);
Parse HTML
You need to "parse" the HTML. There's (at least) two ways to do it:
DOMParser or create an element and stuff the HTML string in there.
The following example shows the second method.
- In my example, I assign some string to
HTML
but you are getting it in some object that you have parsed from JSON. - Create an element. Could be any old element, but I chose
div
- assign the HTML string to the
innerHTML
of the element created in step 2 - use querySelectorAll to select the node you want. In this example, I selected all of the
p
elements. - I simply logged out the second
p
, but in your case, you add it to the page, I assume
let HTML = "<p>something</p><p>else</p>";
let el = document.createElement('div');
el.innerHTML = HTML;
let ps = el.querySelectorAll('p');
console.log(ps[1]);
let HTML = "<p>something</p><p>else</p>";
let el = document.createElement('div');
el.innerHTML = HTML;
let ps = el.querySelectorAll('p');
console.log(ps[1]);
let HTML = "<p>something</p><p>else</p>";
let el = document.createElement('div');
el.innerHTML = HTML;
let ps = el.querySelectorAll('p');
console.log(ps[1]);
edited Nov 16 '18 at 18:22
answered Nov 16 '18 at 5:01
WillWill
1,81911211
1,81911211
That worked!! Thank you Will
– ConwayStern
Nov 16 '18 at 10:43
add a comment |
That worked!! Thank you Will
– ConwayStern
Nov 16 '18 at 10:43
That worked!! Thank you Will
– ConwayStern
Nov 16 '18 at 10:43
That worked!! Thank you Will
– ConwayStern
Nov 16 '18 at 10:43
add a 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.
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%2f53329901%2fhow-to-parse-select-second-html-anchor-element-within-a-json-response%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
Two things that could have happened: 1) you have got the indexes mixed up (likely since there are lots of
p
elements) 2) You've got some undefined variables (what isdescription
?)– Jack Bashford
Nov 16 '18 at 0:54
Hey Jack. That's the thing, description is pretty deep inside the JSON so there's hardly any tags. it is too long to post, but there is only 3 <p> elements and 2 'text' elemetns
– ConwayStern
Nov 16 '18 at 0:59