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.

8
  • Selected is string not json object Commented Oct 16, 2012 at 14:24
  • @Amareswar: selected is a json object. Commented Oct 16, 2012 at 14:43
  • @raina77ow: you should read this: developer.mozilla.org/en-US/docs/DOM/Storage Commented Oct 16, 2012 at 14:44
  • 1
    @Amareswar: There is no such thing as a "json object". Commented Oct 16, 2012 at 15:20
  • 2
    @raina77ow: Well, you got me there :-P It just bugs me a little when people say "json object", it's a "JavaScript object". JSON is a string representation of data that happens to resemble JavaScript object syntax. Commented Oct 16, 2012 at 15:25

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

4 Comments

I had it as map instead of each, until I read here: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: 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.
@exception Why, there's a whole lot of such questions here at SO. Even a special tag for that. )
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.