Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 19th, 2005, 10:01 AM
Authorized User
 
Join Date: Jul 2003
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dynamically Setting X, Y Coordiantes of Element

Hello Everyone and thanks for your help in advance. I am working on an application that utilizes DHTML. There are three textboxes on the webform and I wan to display a panel below each of the textboxes when someone is typing in them. However, I am having problems getting this panel to appear in the correct place. Here is some code sample:

xPos = getxCoordinate(searchBoxName);
yPos = getyCoordinate(searchBoxName);
var hgt = getElementHeight(searchBoxName);

function getxCoordinate(myElement)
{
    var xPos;
    var elem = document.getElementById(myElement);
    xPos = eval(elem).offsetLeft;
    return xPos;
}
function getyCoordinate(myElement)
{
    var yPos;
    var elem = document.getElementById(myElement);
    yPos = eval(elem).offsetTop;
    return yPos;
}
function getElementHeight(myElement)
{
    var height;
    var elem = document.getElementById(myElement);
    height = eval(elem).offsetHeight;
    return height;
}

yPos = yPos + hgt;
resultsPanel.style.top = yPos;
resultsPanel.style.left = xPos;
resultsPanel.style.visibility = "visible";

For some reason, when I check the xPos via an alert, it always returns 1 and the yPos does not function correctly. Any help on this topic would be greatly appreciated. Thanks.


 
Old August 19th, 2005, 03:52 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Not quite sure what you need but you certainly don't need "eval". Here's a golden rule of JavaScript: NEVER USE EVAL.
Code:
function getxCoordinate(ElementId)
{
  return document.getElementById(ElementId).offsetLeft;
}
Personally I would consider just having a div with its style.display set to "none" beneath each textbox and display it when needed, this avoids calculating the position dynamically.

--

Joe (Microsoft MVP - XML)
 
Old August 20th, 2005, 03:18 AM
Friend of Wrox
 
Join Date: Sep 2004
Posts: 103
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If you place the absolute div just before the the text tag in your html it will appear just where u want it to be. In the div itself you can add a <br> or another empty div with proper height to push it down a bit to appear exactly below the text box.
I myself haven't yet found a way to get the x and y absolute position of a none-absolute element on the page :(

 
Old August 20th, 2005, 07:00 AM
Authorized User
 
Join Date: Jul 2003
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the response. What I am trying to accomplish is an "autocomplete" style box that appears below a textbox. The application is an AJAX application that works like Google Suggest. However, it varies in there are actually three textboxes on the page that I am trying to do this with. The results panel is the same data for all three boxes, so it owuld be advantageous to use the same result panel and simply place it based on the coordinates of the textbox being typed in. Since Javascript isn't my strongest language, some of the dynamic placement escapes me. I suspect that the offsetLeft and offsetTop may be based on the parent element and not on the document itself, so that may be the problem. However, I'm stuck since I don't know what the solution is. Any further assistance would be greatly appreciate. Thanks.

 
Old August 21st, 2005, 05:01 AM
Friend of Wrox
 
Join Date: Sep 2004
Posts: 103
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I could guessed what ur problem was and gave u the best solution I had.
If I had the same problem you've got, I would palce 3 div just before each text box and would try to show and hide them via obj.style.display='' or obj.style.display='none'
as I mentioned before to align it vertically just below the text box u'll have to add some other div or a <br> tag in the div you want to show behind the text box...

And again as I mentioned above the problem is that which it's position is not u can't get the absolute position of an item absolute(for example a td in a table).

If anyone knows how to do it please share the solution?






Similar Threads
Thread Thread Starter Forum Replies Last Post
setting public strings dynamically havey C# 16 January 20th, 2008 12:04 PM
Setting onChange Event w/Dynamically Created Form rvanandel Javascript How-To 4 June 28th, 2007 08:10 AM
Setting Gridview Dynamically copelanda BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 1 February 21st, 2007 01:28 PM
Subform - Dynamically Setting mnoon Access 14 March 13th, 2005 07:33 PM
Setting stylesheet for dynamically created object tgopal Javascript 2 September 6th, 2004 11:47 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.