$(...).each is not a function

M. Sa picture M. Sa · Aug 10, 2015 · Viewed 44.6k times · Source

I'm trying to get the text inside all h2 tags on a page, using the web console.

All I've found says to use each, I've tried

var anArray = [];

$('h2').each( function(i,e) {
    anArray.push($(e).innerHTML);
});

But it returns TypeError: $(...).each is not a function.

I've also tried using

$.each('h2', function(i,e) {
        anArray.push($(e).innerHTML);
    });

But again, all I get is TypeError: $.each is not a function?

Answer

Aramil Rey picture Aramil Rey · Aug 10, 2015

1) Paste:

var script = document.createElement('script');
script.src = "https://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(script);

on your console (includes jQuery)

2) Wait 1 sec and paste:

var h2Arr = [];
$('h2').each( function() {
    h2Arr.push($(this).html());
});

Now, all contents of h2 tags of that page should be stored into h2Arr