473,394 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Get position of word from html code given its start and end index calculated from

I have
Expand|Select|Wrap|Line Numbers
  1. contenteditable
div and would like to know the index of character typed in it with respect to html code.
For example:

Expand|Select|Wrap|Line Numbers
  1. <div id="editableDiv" contenteditable="true">
  2.     the @twitter
  3. </div>
The sentence is formatted like this: the @twitter
I can calculate the string position in sentence using below sentence. This is called once @ keyed down (start index) and on space bar key down (end index - 1)

Expand|Select|Wrap|Line Numbers
  1. getCaretPosition : function(){
  2.        var element = document.getElementById("editableDiv");
  3.        var caretOffset = 0;
  4.        if (typeof window.getSelection != "undefined") {
  5.             var range = window.getSelection().getRangeAt(0);
  6.             var preCaretRange = range.cloneRange();
  7.             preCaretRange.selectNodeContents(element);
  8.             preCaretRange.setEnd(range.endContainer, range.endOffset);
  9.             caretOffset = preCaretRange.toString().length;
  10.         } else if (typeof document.selection != "undefined" && document.selection.type != "Control") {
  11.             var textRange = document.selection.createRange();
  12.             var preCaretTextRange = document.body.createTextRange();
  13.             preCaretTextRange.moveToElementText(element);
  14.             preCaretTextRange.setEndPoint("EndToEnd", textRange);
  15.             caretOffset = preCaretTextRange.text.length;
  16.         }
  17.         return caretOffset;
  18.     }
Start index is 5 and end index is 12 for the word @twitter in above sentence.

I would now like to get the index of the same word from innerHTML of the div
Expand|Select|Wrap|Line Numbers
  1. editableDiv
.

InnerHTML:
Expand|Select|Wrap|Line Numbers
  1. <b>the</b> @twitter
Input index of the word @twitter: (5,12)
Output ->
start Index : 12
end index: 19
Dec 31 '13 #1
0 1051

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Travis Pupkin | last post by:
Hi, I use a WYSIWYG rich text editor on a few web sites for clients to manage their content, but a lot of them use word and paste into the box and end up with a lot of crappy code as a result. ...
7
by: Neil Varshneya | last post by:
Hi, I'm looking for an html code list that contains all html codes and their effects. If all of you smart people who read this article know where such a list is please post a follow-up message to...
3
by: Spartanicus | last post by:
How to get help from this group, and how to construct a minimised test case: http://www.spartanicus.utvinternet.ie/help_us_help_you.htm -- Spartanicus
5
by: MK | last post by:
Dear friends, I have many HTML files and they all have some common HTML code which is basically bunch of tags which are in all the files. How can I put the common code in one file and then share...
4
by: Eirik Eldorsen | last post by:
Is it possible to clear the generated html ouput on aspx page, and then write something else? Why? :-) I want to only the html code from a panel, and most important no form tags. Why? I'm...
0
by: shaolinman829 | last post by:
My collegue and I are at the point of pulling our hair out. I'm currently writting a little piece of Java code to send an email confirmation page that sends both html and text. Not a problem. In my...
0
by: dfirka | last post by:
Dear Everyone, Using a RichTextBox I need to replace text based on certain RTF codes found into the RTF string I look for certain embedded (hidden) information using: int i =...
18
by: amritpalpathak | last post by:
I have made the html+javascript code.According to which when the page loaded the image size gets reduced to 50,50 and onclicking on it ,size increases to 500,500,but i done it with the one copy of ...
4
by: amardeep chana | last post by:
Hi, I've spent so long trying to get this to work. First I used this tutorial: http://webdesign.torn.be/tutorials/javascript/prototype/page-loading/ For some reason, it doesn't work for me: ...
1
by: Frank Lovejoy | last post by:
I have googled everywhere but cannot find a solution. Hopefully someone can help me here!? By clicking a trigger image, I want to also pass an index value to my object inside my document.write...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.