Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to calcule text click position 1

Status
Not open for further replies.

Joxemai

Programmer
Mar 5, 2001
12
ES
How can when clicking on a text in which position have been clicked. Should I use textareas? How?
 
IMO why click coordinates over text? If you need some kind of a map, use image and MAP/AREA tags.
 
I exacly must now in which character position (ex: 10th character, 43rd character ...) had been clicked, to know on whitch word was clicked. Realy, I have a text and I nead to control which words are clicked.
Thanks for all
 
Won't work that way... dimensions of characters depend on font, text can wrap etc.
Use TextRange object instead. This should work in IE:
Code:
<div onclick="getClickedWord();">
Some text here blah blah foo bar
</div>

<script language="javascript">
function getClickedWord()
{	oEvent = window.event; 
	oTR = document.body.createTextRange();
	oTR.moveToPoint( oEvent.x, oEvent.y );
	if ( oTR.expand("word")) alert( "Closest word: " + oTR.text );
}
</script>
 
Thank you very very much! It works! But ... Do yoy know how to do the same think to other browsers? I have try it in Mozilla and "ups! doesn't work
 
Where can I find information about how to use TextRange objets?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top