jQuery("body").dblclick(function(event){
var x = event.pageX - this.offsetLeft;
var y = event.pageY - this.offsetTop;
jQuery("<div></div>").addClass("node").css("position","absolute")
.css("top",x).css("left",y).bind("click",showOptions).appendTo("body");
var showOptions = function()
{
alert("santa clara");
}
});
I have two problems here one is that I am not able to get the mousePositions right. Other is that I am wishing to call function showOptions on click of dynamically created div that’s not happening.
The
document.bodyhas nooffsetLeftandoffsetTopas it is the page itself – it starts at(0, 0). Don’t you simply wantevent.pageXandYperhaps? That will also correspond withposition: absolutepixels.Secondly, you define your
showOptionsfunction after you assign it to.bind. So it’s not available yet.I also added some tricks so that the code is a little more readable: