At runtime, I have Javascript create html and I want to bind an object on them to pass onto a function for later.
Something like this:
var projects = //(json object)
projects.forEach(function(p) {
$("#container").append("<div onclick=handleClick(p)></div>");
});
function handleClick(p) {
//...
}
It seems like doing this makes p become the string [object object] instead of the actual Project object and using JQuery's built in $.click function is the same thing. I know I have to bind these elements to a function at runtime somehow.
What is the proper way to do something like this without excessive searching and DOM access?
.data()method to store the object against the element in question. Then in the click handler use$(this).data()to retrieve it. Read the.data()doco and give it a try; come back if you still have trouble.