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
  • 572,313
  • 128
  • 898
  • 1,281
MKN Web Solutions
  • 2,674
  • 3
  • 33
  • 49

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
  • 572,313
  • 128
  • 898
  • 1,281