MiniMe MiniMe - 1 year ago 130
Javascript Question

What could be the cause for "jquery.min.js:2 Uncaught TypeError: Cannot read property 'each' of null"?

Here is the code that I am trying you place in my articles:,console,output

The purpose of this is to offer collapsible paragraphs (DIVs) using

to remember the previous state (e.g. when the reader leaves the page and then returns). Please see the above jsbin link for an example.

At this point if I use the jsbin code it works fine. The problem appears when I place the code in Joomla it does not work anymore. The
function is OK but the
does not seem to work. I think that it has something to do with the document.ready function but I am not sure. What else can I use?

<script type="text/javascript" src=""></script>

<script type="text/javascript">
$( document ).ready(function() {
$(".collapsible").each(function(index) {
if (typeof($(this).attr('id'))!="undefined") {
var state = localStorage.getItem(id)
ele = document.getElementById(id)
if (state=="true") { = 'block'
} else { = 'none'

window.toggleDiv = function(divId) {
var ele = document.getElementById(divId);
var state=$(ele).is(':visible');
localStorage.setItem( divId, state);

a:hover {
color: purple;
a:active {
color: purple;

<a class="ConceptLevel1" href="javascript:toggleDiv('PktS/h+L5EeSqM/4hMH9JA11');"

style="text-decoration: none; font-weight:bold; font-size:13pt">Collapsible divs</a><br>
<div class="collapsible" style="padding-left:15px; " id="PktS/h+L5EeSqM/4hMH9JA11">
<a class="ConceptLevel2" href="javascript:toggleDiv('SPrQVTbDx0WO6As2F+43tw11');" style="text-decoration: none; font-weight:bold; font-size:12pt">hfghg</a><br>
<div class="collapsible" style="padding-left:15px; " id="SPrQVTbDx0WO6As2F+43tw11">

Note: Joomla is already configured not to strip out code inserted into article


This seems to be a JQuery problem.
For some reasons this line


returns this error:

jquery.min.js:2 Uncaught TypeError:
Cannot read property 'each' of null

This happens only when the code is inserted in an article. It seems to me that the selector
returns nothing, hence the result.

Answer Source

Even if jQuery couldn't find .collapsible, it would return an empty collection and would have the each method.

My guess (specially because it only happens in Joomla) is that some other script is taking the $ var. So, things could be happening in this order:

  1. jQuery loads
  2. your code runs, $ is jQuery, so $(document).ready works, but the callback is not executed yet
  3. another script is loaded and assigned to $
  4. now the document is ready, the callback will be executed, and $ is not jQuery anymore

Simplest way to know if that is the case is to replace all $ in your code with jQuery. Example:

jQuery( document ).ready(function() {
   jQuery(".collapsible").each(function(index) { ...

You can also use encapsulation:

    $( document ).ready(function() {
        // ...your code...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download