A more suitable way to do what you want is to store the object in the data
element, which is what jQuery stands for.
Then assign a click
handler that calls your desired function with the saved data.
$(document).ready(function () { var name = { Name: "Stack", lastName:"Exchange" }; var a = $('<a href="#">sendObject</a>').data('name', name).on('click', function(e){ sendObj($(this).data('name')); }); $("#test").append(a); }); function sendObj(e) { console.log(e); }
Demo (welcome console, alert()
ditch).
source share