views:

1369

answers:

3

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.

Many thanks in advance.

+4  A: 
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

Josh Stodola
This works, but I'm hoping there's a way to use jQuery's existing functionality.
Scott Saunders
Mmmmk I updated my answer :)
Josh Stodola
That's it! Two great answers in one. Thanks!
Scott Saunders
A: 

You might find that the issue you're facing is not a positioning issue at all. Based on the syntax you posted, you may be experiencing an IE bug relating to the use of the CSS ID selector.

$("#main").append(newDiv);

If IE doesn't recognize the "#main" element, the append() function will not work correctly. IE (pre-v7) has spotty support for the ID (#) selector. Instead try:

$('div[id="main"]').append(newDiv);

Try this and let me know how it works for you.

Joe Davis
Thanks, but that doesn't do it. event.pageX/Y are definitely undefined. I have confirmed that by alerting them out.
Scott Saunders
Using the ID is the easiest possible way to get an element, and it's been supported since antique versions of IE. There's no way that IE screwed this up. Either provide a link that backs up your claim, or I am calling BS!
Josh Stodola
I didn't say it wasn't supported. I said it had spotty support. And I don't presume that the sample code the asker posts is the exact code from the project. (usually it's cleaned up for public display and simplification) http://reference.sitepoint.com/css/idselector "In Internet Explorer 6, an ID selector is ignored unless it’s the last ID selector in the simple selector". The symptoms here seemed consistent with the element being drawn inline and not "attached" to an existing element. It was worth a shot. Sorry it wasn't the simple fix I was hoping for you, Scott.
Joe Davis
And, Josh, voting my response down without giving me an opportunity to defend myself doesn't feel very friendly. If your goal is to offend, I think you'll do well.
Joe Davis
Not trying to offend anybody! That link has nothing to do with jQuery.
Josh Stodola
A: 

This blog post seems to be relevant: http://www.barneyb.com/barneyblog/2009/04/10/jquery-bind-data/

Seems it's just jQuery("#selector").bind("click", {name: "barney"}, clickHandler);, then e.data.name to access it in the event.

Eric