Friday, July 31, 2009

How can you get a X and Y position of a textbox in HTML?

I need the absolute position of a TextBox in HTML. I use Javascript for scripting.

How can you get a X and Y position of a textbox in HTML?
To get the textbox position, put the following into your code





xpos=document.getElementById( "tb" ).offsetLeft;


ypos=document.getElementById( "tb" ).offsetTop;





where tb is the id of textbox


Here's a demo of the solution http://www.freewebs.com/swhtam/YA/textbo... go to menu View, source for the complete code.





In the demo, use your mouse to push the textbox around, the co-ordination of the textbox will change accordingly, it works on IE, Firefox or Opera.


Hope this helps.
Reply:style="position:absolute" :)
Reply:This willl help you to do this.





function getAbsolutePosition(element){


var ret = new Point();


for(;


element %26amp;%26amp; element != document.body;


ret.translate(element.offsetLeft, element.offsetTop), element = element.offsetParent


);





return ret;


}





function Point(x,y){


this.x = x || 0;


this.y = y || 0;


this.toString = function(){


return '('+this.x+', '+this.y+')';


};


this.translate = function(dx, dy){


this.x += dx || 0;


this.y += dy || 0;


};


this.getX = function(){ return this.x; }


this.getY = function(){ return this.y; }


this.equals = function(anotherpoint){


return anotherpoint.x == this.x %26amp;%26amp; anotherpoint.y == this.y;


};


}








All the Best.

flower arranging

No comments:

Post a Comment