Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

event.pageX - Use jQuery Event in a function not bound through jQuery?

I have a table, and when the user clicks on each cell, some details should appear in a small popup div that appears where the user clicked. I'm using jQuery, but not to bind the function to the onClick event.

function detailPopup(cell, event, groupName, groupID, ...)
{
   var newDiv = document.createElement("div");
   newDiv.id = "detailPop" + groupID;
   newDiv.className = "priceDetailPopup";
   newDiv.innerHTML = "<p>" + groupName + "</p>"; // more will go here
   $(newDiv).click(function()
       {
           $(this).fadeOut("fast").remove();
       }
   );
   $("#main").append(newDiv);
   $(newDiv).css({"left" : event.pageX, "top" : event.pageY}).fadeIn("fast");
}

Everything is working wonderfully in FF, Safari, and Chrome. In IE, it all works except that the detail div appears below the table. event.pageX/Y aren't working. I know jQuery will fix those for IE if I bind the function through jQuery like this:

$(cell).click(function(e) { ... e.pageX ... })

But I can't do that. (I don't think I can - if you do, please explain how I can get six variables into that function without having to use non-xhtml tags in the cell.)

Is there a way to have jQuery "fix" the event object without binding the function through jQuery? $JQuery.fixEvent(event); or something? I can't find any reference to doing so on their site.

like image 296
Scott Saunders Avatar asked Jun 11 '09 17:06

Scott Saunders


People also ask

Which jQuery method is used to attach an event handler function to an HTML element on mouse click?

The click() method attaches an event handler function to an HTML element. The function is executed when the user clicks on the HTML element.

What is bind event in jQuery?

bind() method is used for attaching an event handler directly to elements. Handlers are attached to the currently selected elements in the jQuery object, so those elements must exist at the point the call to . bind() occurs.

Why we use jQuery to wire up events?

jQuery makes it straightforward to set up event-driven responses on page elements. These events are often triggered by the end user's interaction with the page, such as when text is entered into a form element or the mouse pointer is moved.

What is bind event in JavaScript?

The event binding allows you to add an event handler for a specified event so that your chosen JavaScript function will be invoked when that event is triggered for the associated DOM element. This can be used to bind to any event, such as keypress , mouseover or mouseout .


1 Answers

e = jQuery.event.fix(e);  //you should rename your event parameter to "e"

I found the fix function by searching through the jQuery source code.

Alternatively, you could use this to get the mouse coordinates without jQuery...

var posx = 0;
var posy = 0;
if (!e) var e = window.event;
if (e.pageX || e.pageY) {
  posx = e.pageX;
  posy = e.pageY;
}
else if (e.clientX || e.clientY) {
  posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
  posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
}

Via PPK: http://www.quirksmode.org/js/events_properties.html

like image 147
Josh Stodola Avatar answered Sep 24 '22 02:09

Josh Stodola