0

I want to create a serial of text sections each with an unique ID, so that I can link each section to another specified objection in my webpage. Each text section is Selected range like this

a text section selected to be added an unique id

I have try to use rangy library to do it.(see Rangy In google code)。But it merely can give selected text a class name. How to give these an Id?

sandyX
  • 137
  • 10
  • 4
    Can you give us some sample input HTML and sample output HTML? For styling purposes there is no essential difference between ids and classes, and if you can set a class you can also set the id so I'm really not sure what your question is. – Halcyon Oct 01 '13 at 13:00
  • @FritsvanCampen I have just edited – sandyX Oct 01 '13 at 13:08
  • I don't upload this webpage yet. @tymeJV – sandyX Oct 01 '13 at 13:11
  • Sorry do you want to id stuff that a user selects? Or do you want to display areas of text and make them selected? – Papasmile Oct 01 '13 at 16:43
  • Yes, I ant to id stuff that a user selects @Papasmile – sandyX Oct 02 '13 at 00:10
  • OK see that answer on SO http://stackoverflow.com/questions/3545018/selected-text-event-trigger-in-javascript . Then if you want just copy that text into a variable or do some operations on your text directly. Does that work? – Papasmile Oct 02 '13 at 00:28
  • @Papasmile , It does work, to some degree. I mean I can get text section I select, But I can give this section an id tag for further purpose. – sandyX Oct 02 '13 at 01:23
  • since it is not a object it is tough to give an ID. You could store the ID of it's container (eg div) and store start and end position of selected text, then refer to it that way later. – Papasmile Oct 02 '13 at 13:44

1 Answers1

0

Unique ID generator:

var uniqueClientIdCounter = 1;
function uniqueId() { 
   return "unique-client-id-" + uniqueClientIdCounter++; 
}
Andrei
  • 42,814
  • 35
  • 154
  • 218
  • I may not make my question clear. I want to create an id to SELECTED RANGE!! – sandyX Oct 01 '13 at 13:23
  • @user1297181 You should be very attentive when writing your question. What's the problem, you don't know how to get selected text range? – Andrei Oct 01 '13 at 13:28
  • Yes of course I can get selected range, But what I really want to do is to create an ID tag to it. @Andrei – sandyX Oct 02 '13 at 23:11
  • Create object {start: 123, end: 234, id: "123-234"}. Should work out – Andrei Oct 03 '13 at 06:57