Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 208
M. Sa Avatar asked Aug 10 '15 05:08

M. Sa


People also ask

How to solve forEach is not a function?

The "forEach is not a function" error occurs when we call the forEach() method on a value that is not of type array, Map , or Set . To solve the error, make sure to only call the forEach method on arrays, Map or Set objects. Copied!

Is not a function at JavaScript?

The JavaScript exception "is not a function" occurs when there was an attempt to call a value from a function, but the value is not actually a function.

Is not a function error message?

The Javascript error TypeError: "x" is not a function occurs when there is an attempt to call a function on a value or object, which is not actually a function.


2 Answers

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

like image 171
Aramil Rey Avatar answered Oct 05 '22 22:10

Aramil Rey


if you write code like without $() for example

var1.each(function(){}) //its wrong//each function is not defined error  $(var1).each(function(){}) //its right 
like image 42
Ambala Chandrashekar Avatar answered Oct 05 '22 22:10

Ambala Chandrashekar