2
Storage.prototype.setObj = function(key, obj) {
    return this.setItem(key, JSON.stringify(obj))
    ^-----Error in this line: Uncaught TypeError: Accessing selectionDirection on an input element that cannot have a selection
}
var selected = jQuery('input:checkbox.mychkbox:checked').each(function() {
        return this.id;
});
sessionStorage.setObj("savedCollSearch",selected);

I am using jQuery 1.7.2 and Chrome 22. This error shows up as Uncaught Exception in Firefox 16. Search in SO and Google does not help and I have no clue how to resolve this.

I am 100% sure jQuery is loaded properly.

exception
  • 955
  • 2
  • 11
  • 23

1 Answers1

4

This expression...

var selected = jQuery('input:checkbox.mychkbox:checked').each(function() {
  return this.id;
});

... seems to be misused here: it'll return you a jQuery-wrapped collection of checked checkbox elements, which is probably not quite easy to stringify (because of circular references).

(as a sidenote, .each will stop the iteration at the first element which doesn't have an id, or have it set to an empty string, but that doesn't matter much here)

You probably wanted to use this instead:

var selected = jQuery('input:checkbox.mychkbox:checked').map(function() {
  return this.id;
}).get();
raina77ow
  • 103,633
  • 15
  • 192
  • 229
  • I had it as map instead of each, until I read here:http://stackoverflow.com/a/749119/1446848 that map is more memory intensive than each. Can you point me to some documentation on "circular references". Never heard of that before in javascript. – exception Oct 16 '12 at 15:15
  • 3
    @exception: `map` and `each` are different functions used for different purposes. Use the correct one for what you are doing, stop the micro-optimizations! One may be more "memory intensive", but unless you have like 1,000,000 elements, it doesn't matter. – gen_Eric Oct 16 '12 at 15:17
  • 2
    @exception Why, there's [a](http://stackoverflow.com/questions/4816099/chrome-sendrequest-error-typeerror-converting-circular-structure-to-json) [whole](http://stackoverflow.com/questions/11298018/can-anyone-tell-me-why-this-would-be-a-circular-reference-or-at-least-point-me) [lot](http://stackoverflow.com/questions/5410162/javascript-circumvent-error-converting-circular-structure-to-json-when-doing-js) of such questions here at SO. Even a special tag for that. ) – raina77ow Oct 16 '12 at 15:18
  • 3
    @exception Of course, it's memory intensive - `map` is used to _return_ a collection, so it has to store the result somewhere. ) `each`, on the other side, usually just _does_ something for each element of collection. – raina77ow Oct 16 '12 at 15:20