Reputation: 24927
My question is similar to Opening JQuery Ui Dialog in MousePosition, however differs in that I want the Jquery Dialog Box to be shown very close to the button which was clicked. My current issue is that when I set the position
attribute, the Dialog box is show at the bottom of the page when using
$("button.deleteEmailRow").live("click", function (e) {
var target = this
$("#emailAddress").html(this.name); // not imp for my problem
$("#dialog-email-confirm").dialog({
autoOpen: false,
width: 400,
modal: false,
resizable: false,
dialogClass: 'dialogs-only',
buttons: {
"Delete Email Recipient": function () {//snip;
return false;},
"Cancel": function () {//snip;
return false;
}
}
});
$("#dialog-email-confirm")
.dialog('option', 'position', [e.pageX+30, e.pageY])
.dialog('open');
});
However, when using the following, it works as intended.
$("#dialog-email-confirm")
.dialog('option', 'position', [e.clientX+30, e.clientY])
.dialog('open');
According to the Jquery website, the first script should work.
When tracking mouse movement, you usually need to know the actual position of the mouse pointer. The event object that is passed to the handler contains some information about the mouse coordinates. Properties such as .clientX, .offsetX, and .pageX are available, but support for them differs between browsers. Fortunately, jQuery normalizes the .pageX and .pageY properties so that they can be used in all browsers. These properties provide the X and Y coordinates of the mouse pointer relative to the top-left corner of the document, as illustrated in the example output above.
I checked to make sure that values are the same using the following and found that there was a a difference with regards to the Y-coordinates.
var pageCoords = "( " + e.pageX + ", " + e.pageY + " )";
var clientCoords = "( " + e.clientX + ", " + e.clientY + " )";
alert(pageCoords + '\n' + clientCoords);
I have read through QuirksMode, and also played around with some test code which behaves as expected. Why is above producing difference results in my script? Is is related to my HTML structure?
Edit (Updated HTML):
<html>
<head>
<meta name="generator" content="HTML Tidy for Windows (vers 14 February 2006), see www.w3.org">
<title></title>
<link href="/Content/Site.css" rel="stylesheet" type="text/css">
<link href="/Content/960.css" rel="stylesheet" type="text/css">
<link href="/Content/themes/liberty/jquery-ui.css" rel="stylesheet" type="text/css">
<link href="/Content/themes/dialogs-only/jquery-ui-1.8.14.custom.css" rel="stylesheet" type="text/css">
<link href="/Content/sexybuttons.css" rel="stylesheet" type="text/css">
<link href="/Scripts/messagebar/skins/plain/skin.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="page">
<div id="header" class="container_12"></div>
<div id="menuwrapper"></div>
<div id="content" class="container_12">
<div id="main" class="grid_12 alpha omega">
<h2>
Configuration Settings
</h2>
<form action="/Settings/Edit" method="post">
<fieldset>
<legend>Settings</legend> // snip
<div class="editor-label">
<label for="EmailSuccessList">List of recipients for Successful Notifications</label>
</div>
<div class="editor-field">
<fieldset>
<input type="hidden" name="EmailSuccessList.index" autocomplete="off" value="4eedd5a0-076b-4bc6-9a07-7455df9c5d83"><input type="hidden" name="EmailSuccessList.index" autocomplete="off" value="151d8ad0-f204-445c-8778-5207ddbae952"><input type="hidden" name="EmailSuccessList.index" autocomplete="off" value="7b51d4b0-78b6-4ccd-a886-ffef9fe8a00f"><input type="hidden" name="EmailSuccessList.index" autocomplete="off" value="0c7f4c5f-7344-4b47-9202-3ff61ce843fc">
<table id="editorEmailSuccessRows" class="editorEmailSuccessRows">
<tr class="editorRow">
<td class="emailRow">
<input class="text-box single-line" id="EmailSuccessList_4eedd5a0-076b-4bc6-9a07-7455df9c5d83_" name="EmailSuccessList[4eedd5a0-076b-4bc6-9a07-7455df9c5d83]" type="text" value="[email protected]">
</td>
<td>
<button type="button" href="#" id="deleteEmailRow" class="deleteEmailRow" name="[email protected]"><span><span><span class="delete">delete</span></span></span></button>
</td>
</tr>
<tr class="editorRow">
<td class="emailRow">
<input class="text-box single-line" id="EmailSuccessList_151d8ad0-f204-445c-8778-5207ddbae952_" name="EmailSuccessList[151d8ad0-f204-445c-8778-5207ddbae952]" type="text" value="[email protected]">
</td>
<td>
<button type="button" href="#" id="deleteEmailRow" class="deleteEmailRow" name="[email protected]"><span><span><span class="delete">delete</span></span></span></button>
</td>
</tr>
<tr class="editorRow">
<td class="emailRow">
<input class="text-box single-line" id="EmailSuccessList_7b51d4b0-78b6-4ccd-a886-ffef9fe8a00f_" name="EmailSuccessList[7b51d4b0-78b6-4ccd-a886-ffef9fe8a00f]" type="text" value="[email protected]">
</td>
<td>
<button type="button" href="#" id="deleteEmailRow" class="deleteEmailRow" name="[email protected]"><span><span><span class="delete">delete</span></span></span></button>
</td>
</tr>
<tr class="editorRow">
<td class="emailRow">
<input class="text-box single-line" id="EmailSuccessList_0c7f4c5f-7344-4b47-9202-3ff61ce843fc_" name="EmailSuccessList[0c7f4c5f-7344-4b47-9202-3ff61ce843fc]" type="text" value="[email protected]">
</td>
<td>
<button type="button" href="#" id="deleteEmailRow" class="deleteEmailRow" name="[email protected]"><span><span><span class="delete">delete</span></span></span></button>
</td>
</tr>
<tr>
<td colspan="3" align="right" class="lastRow">
<a href='/Settings/BlankEmailEditorRow?collectionName=EmailSuccessList' class=" sexysimple sexygreen" id='addNewSuccessEmail'><span class="add">Add new recipient</span></a>
</td>
</tr>
</table>
</fieldset>
</div><button type="submit" value="Save Configuration" id="save" name="save"></button> <a href="/Settings" id="cancelEdit">Cancel</a>
<div class="cancel" style="display: none">
<div id="dialog-confirm-cancel" title="Cancel Edit?" class="diag">
<p>
Any changes you have made will not be saved.
</p>
</div>
</div>
<div class="demo" style="display: none">
<div id="dialog-email-confirm" title="Delete Email?" class="dialogs-only">
<p>
Are you sure you wish to delete this recipient from the list : <span id="emailAddress" style="font-weight: bold;"></span>?
</p>
</div>
</div>
</fieldset>
</form>
<div id="footer"></div>
</div>
</div>
</div>
</body>
</html>
Tests using Google Chrome v14
Refer to http://jsfiddle.net/SUYA7/ for full sample.
Upvotes: 1
Views: 1892
Reputation: 43823
Although not the cause of the problem, there are multiple elements with the same id
which is invalid HTML. The following markup occurs 4 times in the example:
<button type="button" href="#" id="deleteEmailRow" class="deleteEmailRow button sexybutton" name="abc.test@com"><span><span><span class="delete">delete</span></span></span></button>
The problem is that the position
option in the jQuery UI dialog needs
an array containing an x,y coordinate pair in pixel offset from left, top corner of viewport (e.g. [350,100])
so you cannot use <event.pageX, event.pageY>
since they provide the X and Y coordinates of the mouse pointer relative to the top-left corner of the document. You need to provide client co-ordinates.
Upvotes: 1
Reputation: 10780
With pageX/pageY you are using document/page relative position but jQuery dialog expects position relative to the viewport.
Upvotes: 1