Javasript websocket.send sends [Object object]

Tags: ,



I have a requirement, where I need to send all user actions in browser to a remote application. Like user hits a link, types in textfield / textarea, selects a choice etc. On browser side I am using Javascript to listen to events and then sending those to remove application using websocket. My code looks as below.

var sock = new WebSocket("ws://192.168.1.2:8082", "tci"); 

function user_intercept_click_event(e)
{
    var target = e.target || e.srcElement;
    sock.send(target);
    console.log(target);
}

function user_intercept_key_press(e)
{
    var target = e.target || e.srcElement;
    sock.send(target);
    console.log(target);
}

if(document.addEventListener) 
{
    document.addEventListener('click', user_intercept_click_event);
    document.addEventListener('keypress', user_intercept_key_press);
} 
else if(document.attachEvent) 
{
    document.attachEvent('onclick', user_intercept_click_event);
    document.attachEvent('onkeypress', user_intercept_key_press);
}

window.onbeforeunload = function(evt) 
{
    sock.close();
    return null;
}

Let’s say I click on a textfield with id as endpoints1, then the console log prints proper value, likeenter image description here

Where as in remote application I get
[object HTMLInputElement]

I want to get complete string as printed in console log in remote application. How can I get it? Any help highly appreciated.

Answer

Try:

sock.send(target.outerHTML);


Source: stackoverflow