| | |
Find location of highlighted text
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
I'm working on a website that's basically a forum software, but with a different arrangement. There are only 3 levels of posts (questions, solutions, and comments), and for the comments I want to be able to have the user highlight part of a solution to comment on. To do that, I want to just have them highlight part of the reply and it will automatically recognize the targeted area.
I've been able to get the text value using document.selection.somethingOrOther, but it does nothing about the actual position of the text in the page. The problem is that if the page contains "foobar fubar foobar" and the user highlights the second foobar, I can't tell the difference between highlighting the first.
Any help is appreciated, and I'm only worried about IE compatability (odd request, I know
), so hopefully there's something hackish out there...
I've been able to get the text value using document.selection.somethingOrOther, but it does nothing about the actual position of the text in the page. The problem is that if the page contains "foobar fubar foobar" and the user highlights the second foobar, I can't tell the difference between highlighting the first.
Any help is appreciated, and I'm only worried about IE compatability (odd request, I know
), so hopefully there's something hackish out there... Here you go MS Boy... :-)
The above is just a small example on what you can do with the API. Once the text is selected, you get its offset in pixels which you can then use to distinguish between the first and second foobar.
If my snippet doesn't give you what you need, also read on selection and range.
Do let me know if it works.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<html> <head> <script> function boundDim(oObject) { var oTextRange = document.selection.createRange(); if (oTextRange != null) { alert("The bounding left is \n" + oTextRange.boundingLeft); oTextRange.pasteHTML("<b>" + oTextRange.htmlText + "</b>"); } } </script> </head> <body onclick="boundDim(this);">Hello to all. Hello to me.</body> </html>
If my snippet doesn't give you what you need, also read on selection and range.
Do let me know if it works.
I don't accept change; I don't deserve to live.
![]() |
Similar Threads
- Parsing html form. (PHP)
- Win2000Pro Desktop Black Screen Message Virus Warning Fix? (Viruses, Spyware and other Nasties)
- To read color highlighted text in word document from vb.net (VB.NET)
- Text to Speech (Visual Basic 4 / 5 / 6)
- Tkinter Text tags (Python)
- The Datagrid: How does one fill text boxes in edit mode with their original content? (ASP.NET)
- HEX location (Windows 95 / 98 / Me)
- Cannot enter any text in a IE text-box (Viruses, Spyware and other Nasties)
- C++ Syntax (C++)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: How do you read radio buttons in XHTML
- Next Thread: Frameset and setTimeout
| Thread Tools | Search this Thread |
acid2 ajax ajaxexample ajaxjspservlets array browser captchaformproblem cart checkbox child class close codes css date debugger decimal dependent design disablefirebug dom editor element embed engine enter events explorer ext file firefox focus form forms frameworks getselection google gxt hiddenvalue highlightedword hint html ie7 ie8 iframe index internet java javascript javascripthelp2020 jquery jsf jsfile jsp jump libcurl listbox maps masterpage math media menu mp4 object onerror onmouseoutdivproblem onmouseover onreadystatechange parent paypal pdf php position post programming prototype rated redirect runtime safari scale scriptlets scroll search security session shopping size software star starrating stars synchronous toggle unicode variables web webservice wysiwyg \n






