Jump to content

[SOLVED] Finding X, Y location of an Element Tag on a page


ainoy31

Recommended Posts

Hello-

 

I need help with my script to find the (x,y) coordinates of an element tag on a page.  Here is my javascript:

function getPos()
{
var elementToFind = document.getElementById('cancelTrigger');
var elementLocs = findPos(elementToFind);
alert(elementLocs[0] + ' ' + elementLocs[1]);
}

function findPos(obj)
{
var curleft = curtop = 0;
if(obj.offsetParent)
{
	curleft = obj.offsetLeft
	curtop = obj.offsetTop

	while(obj = obj.offsetParent)
	{
		curleft += obj.offsetLeft
		curtop += obj.offsetTop
	}
}

return [curleft, curtop];
}

 

 

Here's where I am calling this:

<tr>
<td colspan="*" valign="bottom">
<input type="text" name="future_cancel_date" value="" id="futureCancelDate" readonly="1"> 
<img src="/images-system/ico_date.gif" border="0" id="cancelTrigger" style="cursor: pointer;" title="Date selector" onClick="getPos();">
 <a href="#" onclick="javascript:document.cancelform.future_cancel_date.value=''"><?= $langContent[$section]['clear_cancellation_date'] ?></a>
</td>
</tr>

 

I am able to get the x and y locations such as 152, 722.  My issue is to pass these values in the position option of the Calendar.setup() javascript function as below:

<script type="text/javascript">
Calendar.setup({
inputField     :    "futureCancelDate",    	// id of the input field
ifFormat       :    "<?= str_replace(array('Y', 'y', 'm', 'd'), array('%Y', '%y', '%m', '%d'), $locale->getDateFormat()) ?>",      		// format of the input field
button         :    "cancelTrigger",  	// trigger for the calendar (button ID)
singleClick     :    true,
             position        :    [x, y]  //x=152 and y=722

});

</script>

 

The reason why I need to figure the x,y is because IE is not positioning the popup calendar properly by using the align option.  the popup calendar displays somewhere else but it needs to be next to my calendar icon.  I hope this is easy to follow and understand.  Much appreciation.  AM

 

 

 

Link to comment
Share on other sites

lemmin-

ok,  if i do alert(elementLocs) in the getPos() function i get my coordinates in an array of [152, 722], which is good.  Either i have been coding too long or something, i still am having an issue passing this array into the Calendar.setup() function for the position option.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.