diff --git a/dhtml_menu.js b/dhtml_menu.js
index 9d38f64..00fbeec 100644
--- a/dhtml_menu.js
+++ b/dhtml_menu.js
@@ -155,8 +155,9 @@ Drupal.dhtmlMenu.cookieGet = function() {
  */
 Drupal.dhtmlMenu.cookieSet = function() {
   var expanded = new Array();
-  $('li.expanded').each(function() {
-    expanded.push($(this).find('a:first').attr('id').substr(5));
+  $('li.dhtml-menu.expanded').each(function() {
+    var id = $(this).find('a:first').attr('id');
+    expanded.push(id ? id.substr(5)  : '');
   });
   document.cookie = 'dhtml_menu=' + expanded.join(',') + ';path=/';
 }
