Sending DOM element from client to server

Viewed 45

I tried to clone a DOM object and send it to the server using socket.io and express, but after copying the element

( const element = document.getElementById('id').cloneNode; ) my server receives an empty object {}

and when i do console.log(element); on the client side everything works

(I send it into server by:

socket.on('document', (data, callback) => {
callback( document.getElementById(data[2]).cloneNode(true) );
})
1 Answers

Socket.io converts objects to JSON to send over the wire. DOM elements have no properties that will convert to JSON.

const body = JSON.stringify(document.body);
console.log(body);

You'll need to copy the values you care about into a new object and convert any non-JSON data types into something that JSON can represent.

Related