0

I've got javascript object that holds a few depths of data. One of the depths has literal dom elements attached. I need to post this object (I'm using jQuery's post func), but I get an illegal invocation because of those dom elements. Is there a quick way to remove those dom elements without having to loop through the object / array?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Michael Mikhjian
  • 2,760
  • 4
  • 36
  • 51

1 Answers1

1

Yes, there is. When serializing the Objects to JSON, you can use a replacer predicate to remove or modify arbitrary data. In your case it would look like this:

$.ajax({
    url: "…",
    data: JSON.stringify(data, function(p,o) {
        if (o instanceof HTMLElement) return null;
        return o;
    }),
    …
});

For more thorough ways of detecting DOM elements see JavaScript isDOM -- How do you check if a JavaScript Object is a DOM Object?.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375