Greasy Fork is available in English.

Discuții » Dezvoltare

[SOLVED] Problems with getting text in class

§
Postat în: 29-12-2016
Editat în: 29-12-2016

[SOLVED] Problems with getting text in class

Hi, I'm trying to write a script to extract the comments from 9gag.com/gag/* pages. (e.g. link ). I'm using the code below. I tried solving this with jQuery but this failed as well.

(function() { 'use strict'; var elements = document.getElementsByClassName('content badge-content'); console.log(elements); console.log(elements.length); for (var i = 0; i < elements.length; i++) { console.log(i); var text = elements[i].innerText; console.log(text); } })();

I attached Chromes console output below... Whereas elements seems to be 19 elements long, elements.length gives 0. So my for loop is not running...

Do you have any ideas??? Thanks for any help ;)

EDIT: Adding an Event listener solved the problem.

window.addEventListener("load", function() {

}
§
Postat în: 29-12-2016

Oh and element[0].innerText failed with the error: cannot get innerText from undefined.

Postează un raspuns

Autentifică-te pentru a posta un răspuns.