0

I am writing a script that stores user mouse clicks on a page using jQuery and bidding the various events, everything is working except being able to store the event.target as a reusable string in mysql. (The info gets passed back through an Ajax call)

How can I get the target as a string that I can use as a jQuery selector. The event.target.id would be an obvious one except there is no guarantee that all click targets have them.

fiscme
  • 422
  • 1
  • 6
  • 20

1 Answers1

1

I think that you could get what html element has been click, but then you have to check out how to find that element inside of the DOM tree, since you're only going to have the html string of the $(event.target).

var arrayElementClicked = [];
$(document).on("click","body",function(){
      $elementClicked = $(event.target)[0].outerHTML; //string of the DOM click
      arrayElementClicked.push($elementClicked);
});

now you have an array of string with the element you have clicked but I dont know if taht exactly what are you looking for.

The other idea would be actually save the event object BUT the problem its that if you want to save the DOM ELEMENT as json format you will not be able to do JSON.stringify(domElement) since has a circular reference, read about that here Chrome sendrequest error: TypeError: Converting circular structure to JSON and people trying to solve that read here JSON.stringify an Array of HTML Elements/Strings

if you solve that you could easily save the event and reproduce later like:

var arrayElementClicked = [];
$(document).on("click","body",function(){
      $elementClicked = $(event.target);
      arrayElementClicked.push($elementClicked);
});

and then do something with AJAX like sending the ARRAY.

Community
  • 1
  • 1
ncubica
  • 8,169
  • 9
  • 54
  • 72