Alter user-selected text

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • helimeef
    New Member
    • Sep 2007
    • 77

    Alter user-selected text

    Hi, I am trying to create a little JavaScript WYSIWYG editor much like the one I'm using to write this thread right now. I have almost enough knowledge of the language to create one. My only question is, how can I retrieve and edit the text a user has selected? For example, in this WYSIWYG editor that I'm using you can select text and make it a hyperlink. How can I modify the text that a user selects?
    Thank you.
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

    have a look here ... may be that helps :)

    kind regards

    Comment

    • helimeef
      New Member
      • Sep 2007
      • 77

      #3
      Originally posted by gits
      hi ...

      have a look here ... may be that helps :)

      kind regards
      Thanks for the help, but that page isn't completed, and I already knew how to retrieve the text a user has selected, I just need to know how to change it. I don't mean just edit a variable storing the selection, I mean actually changing the text that was selected on the page.
      Thanks though! :D

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        change to what?

        kind regards

        Comment

        • helimeef
          New Member
          • Sep 2007
          • 77

          #5
          Originally posted by gits
          change to what?

          kind regards
          Say that a simple page looks like this (forget the WYSIWYG thing):
          [CODE=html]
          <body>
          <p>I am a simple page</p>
          <button id="changeSelec ted">Change selected text</button>
          </body>
          [/CODE]
          And the user selects/highlights "I am" in the first <p> and then clicks the button, and the text that they selected is changed to like "I am not", making the generated source look like this:
          [CODE=html]
          <body>
          <p>I am not a simple page</p>
          <button id="changeSelec ted">Change selected text</button>
          </body>
          [/CODE]
          How can I change the text on the page that a user has selected.
          The reason I'm asking this is because say that in the WYSIWYG editor, somebody selects text and clicks the bold button. I then want the JavaScript to store the selected text in a variable, add <strong> tags around it, and then replace the selected text with new bolded text.
          I hope I'm making myself clear...

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            this are two different things but you may handle them the same way if you wish:

            1. editing the selected text:

            idea is to replace the selected text with the edited text ... identify the start and the end of the range, remove the characters here from range start to end and insert the new text

            2. add tags:

            idea is ... identify the start and the end of the range (same as above) but simply add the respective tag-text to the current text at the range start and end positions

            kind regards

            Comment

            • helimeef
              New Member
              • Sep 2007
              • 77

              #7
              Originally posted by gits
              this are two different things but you may handle them the same way if you wish:

              1. editing the selected text:

              idea is to replace the selected text with the edited text ... identify the start and the end of the range, remove the characters here from range start to end and insert the new text

              2. add tags:

              idea is ... identify the start and the end of the range (same as above) but simply add the respective tag-text to the current text at the range start and end positions

              kind regards
              Okay I went ahead and made the range and used a for loop to list its properties. Now I found startOffset and endOffset in Firefox... How can I replace only the text between those two values? I can imagine string.replace but that doesn't allow you to specify a start and end point. Substring does but you do not edit anything, just returns a value. What function should I use?
              Kind regards (hehe)

              Comment

              • gits
                Recognized Expert Moderator Expert
                • May 2007
                • 5390

                #8
                the substring method should do it :)

                [CODE=javascript]var text = 'foo bar foobar';
                var new_selected = 'newbar';
                var new_text = text.substr(0, 4) + new_selected + text.substr(7, text.length);
                [/CODE]
                now you could replace the entire text of the html node with new_text ...

                kind regards
                Last edited by gits; Dec 27 '07, 08:44 PM. Reason: typo

                Comment

                • helimeef
                  New Member
                  • Sep 2007
                  • 77

                  #9
                  Originally posted by gits
                  the substring method should do it :)

                  [CODE=javascript]var text = 'foo bar foobar';
                  var new_selected = 'newbar';
                  var new_text = text.substr(0, 4) + new_selected + text.substr(7, text.length);
                  [/CODE]
                  now you could replace the entire text of the html node with new_text ...

                  kind regards
                  Oh duh :) Never thought of that XD
                  Happy new year :)

                  Comment

                  • gits
                    Recognized Expert Moderator Expert
                    • May 2007
                    • 5390

                    #10
                    happy new year to you too ;) ... post back to the forum in case you have more questions ...

                    kind regards

                    Comment

                    Working...