[solved] Need an assist determining an object's placement...

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

[solved] Need an assist determining an object's placement...

Post by Burrito »

I am writing an ajax autoselect for textfields (and I will share it when I'm done :) ). I'm having a wee bit of a challenge though figuring out how to determine my object's placement on the page (text field object).

I need this so I can drop my selections directly below it. I've discovered that the text fields do not have the top nor left style properties as I expected they would :x

thus:

Code: Select all

<script>
function dropIn(obj)
{
     alert(obj.style.left+" "+obj.style.top);
}
</script>
<input type="text" name="somefield" onKeyUp="dropIn(this)">
will NOT work. That doesn't throw an error, it just alerts blanks.

anyone know a way to determine a text field's placement on the page dynamically?
Last edited by Burrito on Mon Jan 02, 2006 11:31 am, edited 1 time in total.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

Nevermind, I figured it:

Code: Select all

function getLeft(obj)
{
	var left = 0;
	while (obj)
	{
		left += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	return left;
}
function getTop(obj)
{
	var top = 0;
	while (obj)
	{
		top += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return top;
}
thx TD :D
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Code: Select all

<html>
<head>
<script>
function dropIn(obj)
{
     alert(obj.style.left);
}
</script>
</head>
<body>
<input type="text" name="somefield" onKeyUp="dropIn(this)" style="position:absolute;left:100px">
</body>
</html>

this works for me in FF 1.0.7


edit: oh you got it..
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

I suspect it would work in IE too if you define the properties on the object itself...but that would be a huge friggin' nightmare to do, not to mention that I don't want all of my text fields absolutely positioned...

the solution I posted works a peach, thx though :P
Post Reply