How to parse select second HTML anchor element within a JSON response










0















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);









share|improve this question






















  • 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
















0















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);









share|improve this question






















  • 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














0












0








0








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);









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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


















  • 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

















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













1 Answer
1






active

oldest

votes


















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.



  1. In my example, I assign some string to HTML but you are getting it in some object that you have parsed from JSON.

  2. Create an element. Could be any old element, but I chose div

  3. assign the HTML string to the innerHTML of the element created in step 2

  4. use querySelectorAll to select the node you want. In this example, I selected all of the p elements.

  5. 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]);








share|improve this answer

























  • That worked!! Thank you Will

    – ConwayStern
    Nov 16 '18 at 10:43










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%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









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.



  1. In my example, I assign some string to HTML but you are getting it in some object that you have parsed from JSON.

  2. Create an element. Could be any old element, but I chose div

  3. assign the HTML string to the innerHTML of the element created in step 2

  4. use querySelectorAll to select the node you want. In this example, I selected all of the p elements.

  5. 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]);








share|improve this answer

























  • That worked!! Thank you Will

    – ConwayStern
    Nov 16 '18 at 10:43















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.



  1. In my example, I assign some string to HTML but you are getting it in some object that you have parsed from JSON.

  2. Create an element. Could be any old element, but I chose div

  3. assign the HTML string to the innerHTML of the element created in step 2

  4. use querySelectorAll to select the node you want. In this example, I selected all of the p elements.

  5. 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]);








share|improve this answer

























  • That worked!! Thank you Will

    – ConwayStern
    Nov 16 '18 at 10:43













1












1








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.



  1. In my example, I assign some string to HTML but you are getting it in some object that you have parsed from JSON.

  2. Create an element. Could be any old element, but I chose div

  3. assign the HTML string to the innerHTML of the element created in step 2

  4. use querySelectorAll to select the node you want. In this example, I selected all of the p elements.

  5. 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]);








share|improve this answer















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.



  1. In my example, I assign some string to HTML but you are getting it in some object that you have parsed from JSON.

  2. Create an element. Could be any old element, but I chose div

  3. assign the HTML string to the innerHTML of the element created in step 2

  4. use querySelectorAll to select the node you want. In this example, I selected all of the p elements.

  5. 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]);






share|improve this answer














share|improve this answer



share|improve this answer








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

















  • 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



















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.




draft saved


draft discarded














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





















































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

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

Evgeni Malkin