Click to See Complete Forum and Search --> : Geting the position of the inputbox


kayzee
02-02-2003, 01:14 AM
Hi all

I am trying write a small tool tip that appears in the Top left corner of all the text boxex indicating the length of the field. (specifically for TextArea tag)

I wrote following script to do that. (simply copy the code to a new HTML file and yopu will see how it works.)

But the Problem starts when I place my InputFields within a table. The left and right cordinates start giving me the cordinates for the able. If any one could tell me how to get the cordinates for any given field (IE only I need this for a Intranet app.) I'd be really greatful

Cheeers

kayzee



<script language="javaScript">
//this function is to display the counter for any given text area
function tooltip(strVal,intMax){
document.getElementById('spnToolTip').style.position = 'absolute';
document.getElementById('spnToolTip').style.left = strVal.offsetLeft + strVal.offsetWidth-10;
document.getElementById('spnToolTip').style.top = strVal.offsetTop - 15;

if (strVal.value.length < intMax)
document.getElementById('spnToolTip').style.backgroundColor = 'yellow';
else
document.getElementById('spnToolTip').style.backgroundColor = 'red';

document.getElementById('spnToolTip').innerHTML.display='';
document.getElementById('spnToolTip').innerHTML = 'len ' +strVal.value.length + '/'+ intMax;

text1.clientLeft
}
</script>
<SPAN ID="spnToolTip"> </SPAN>

<input name=text1 onkeyup="tooltip(text1,10);" >

kayzee
02-02-2003, 08:23 AM
Dave..

Thaks for your reply.., but unfortunatly I did not get it clearly. (may be i am too dumb :) )

I dont have a problem in positioning the image/or object, my issue is in retreiving the position of the object.

What I need is to get the cordinates of a given object.

I found that you are using the ofsetlef and offssetTop. But if you run the script I send to you you will notice what I mean.

Thanks

Kayzee

khalidali63
02-02-2003, 08:39 AM
Your problem is the "offsetLeft".
The expected behavior is to get the left co-ordinates relative to the parent element of the current object.

And when you put it in a table then then it gets the co-ords relativeto the table.

this miht be a good place for you to look for a work around.

http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/properties/offsetleft.asp

cheers

Khalid

kayzee
02-02-2003, 08:52 AM
Hi Khalid

Thanks for the reply , Yes thats right.? I know that is the problem.

I also was in that web site for hours before coming to this forum. I was trying to calculate the cordinates based on the following diagram.

http://msdn.microsoft.com/workshop/author/om/measuring.asp

But some how the actual behaviour of the browser is very different from what the diagram shows. (at least when we have tables)

I never thought getting a simple value like this would need so much of coding.

Considering I have to write a function that "Nest-out" the nested objects and calculate the cordinates, and then when I have to do the same for each and every key stroke, this does not sound like very robust way to do this

Dont they have a better way to do it ..?

Cheers

Kay

khalidali63
02-02-2003, 09:08 AM
Interesting,off the top of my head I think you might end up defining the co-ords manually such as margin-top:40pt;

But thats hard coding and don't know if hard coding co-ords is an option for you.

Khalid

kayzee
02-03-2003, 04:11 AM
Yes, That’s what I want.

But as I can see in that code we need to go through a loop up until the top parent object.

Considering I have to do this in each and every keystroke, Will this be a "Feasible" approach?

Some how I will try to implement this and see how the Performance is.

Thanks for the support.

Cheers

Kuminda

kayzee
02-03-2003, 04:25 AM
hmmm.. I did that.. Cannot see any significant performance issue.

Lets hope users want use 486 50Mhz machines any more ...
:)


thanks both of you for the support
Cheers

kay

kayzee
02-03-2003, 06:00 AM
I am doing some modifications to an application developed by about 4 years a go and mod by 6 or more people ever since.

There is a quite anumber of <TextArea> tags that they used to input various comments. The user says they need to know how mnay more texcts they can type so they can organise there comments acordingly.

So I am trying show a number of characters in the same text box. I need to do this with minimum changes to existing forms.

So each key stroke is we need to caculate the number of characters. I thought storing the locations in an array or something. But I dopnt want to have any mods on page level. (i am trying to do it in field level)

Somehow it seems working now ..

Cheers

kayzee
02-03-2003, 06:24 AM
Yes, Thats right.

But there are somany text areas and they will switch the text areas frequently,

I probbly will can keep the current field name in memory and then check for that. thats nice! you keep me thinking..!!!

I can try that ofcourse, thanks for the tip

Cheers