Skip to content
Advertisement

Trying to pull data from a table using jQuery

Just to give a background of the question, I am trying to pull data from an html website which was made using tables. I have managed to pull most of them but there’s just one thing which is troubling my brains. Maybe I need a break from work?

I have included all the code in a fiddle which can be found here. https://jsfiddle.net/ex1j6gr4/

Basically I am trying to pull the article date and author from that particular . So I am looping through the in that and getting the element which has the date and the author using certain keywords. Using font:nth-child is not possible because not all the count of tag is not the same in every page. (You can see two empty ones in the jsfiddle table which was a mistake)

For the date, I have made an array of the month names and its easy to pull through that.

For the author, I am detecting the first word of that element’s text which is “By” and its doing its job as well.

However the problem I am facing is when I am using that element outside the “.each” function which is returning the value as “undefined”. Here’s the jQuery code I am using.

JavaScript

Any help would be greatly appreciated !

Advertisement

Answer

You needed to define your variables outside of your functions (you had 2 loops and the second was trying to reference variables defined outside of it’s scope). Here I’ve combined the 2 loops, removed many of the var – you only need to define that once and then you can reference the actual variable after that.

Finally, jQuery couldn’t find ('td') unless it was actually sitting inside a <table> tag. I didn’t have a function you were referencing so I put in a little forEach loop to test for the month.

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement