remove selected text

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oll3i
    Contributor
    • Mar 2007
    • 679

    remove selected text

    How do I remove selected text in javascript?
    Thank You
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

    please explain that a bit more ... what do you select? is it a specific widget (texbox, textarea or what)?

    kind regards

    Comment

    • oll3i
      Contributor
      • Mar 2007
      • 679

      #3
      i need to remove a text selected in the editable div
      offer=offer.rep lace(txt,'');do esnt work where txt is a passed selected text
      i have other function for it
      thank You

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        do you already have the selected text retrieved? do you use something like ranges for that? ...

        kind regards

        Comment

        • oll3i
          Contributor
          • Mar 2007
          • 679

          #5
          Code:
           function getSelectedText(){
           var txt = '';
          if (document.getSelection) txt = document.getSelection();
          else if (document.selection) txt = document.selection.createRange().text;
          
          return txt;
          
           }
          here is my code to get the selected text
          i know it works
          just the selected text is not replaced with ''
          thank You

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            so the replace is not working? ... show function you use here. you have to retrieve the text ... replace it and reapply it with innerHTML for example ...

            kind regards

            Comment

            • oll3i
              Contributor
              • Mar 2007
              • 679

              #7
              yes the replace is not working
              function where i try to replace selected text and then make it an unordered list is
              Code:
               function makeUnorderedList(txt){
               var tempText="";
               var textToRemove="";	
               var splitResult = txt.split('\n');
               var offer=document.getElementById("editor_displaying_text").innerHTML;
                
               
                offer=offer.replace(txt,''); ,<---this doesnt work
              
              
              
               
               for(i = 0; i < splitResult.length; i++){
              	tempText=splitResult[i];
              	tempText=tempText.replace(splitResult[i],'<li>'+splitResult[i]+'</li>');
              	textToRemove+=tempText;
              	offer+=tempText;	
              }
               
              offer=offer.replace(textToRemove,'<ul>'+textToRemove+'</ul>');
              document.getElementById("editor_displaying_text").innerHTML = offer;
              }

              Comment

              • oll3i
                Contributor
                • Mar 2007
                • 679

                #8
                the unordered list is created:) but the selected text from the div with id "editor_display ing_text" is not removed
                thank You

                Comment

                • gits
                  Recognized Expert Moderator Expert
                  • May 2007
                  • 5390

                  #9
                  hi ...

                  do you get an error-message? the following example with your code seems to work:

                  [CODE=javascript]
                  function makeUnorderedLi st(txt) {
                  var tempText = "";
                  var textToRemove = "";
                  var splitResult = txt.split('\n') ;
                  var offer = 'test\nabcd\nab cd\nabcd\nabcd\ nabcd\nabcd\nte st1\n';

                  offer = offer.replace(t xt,'');

                  for(i = 0; i < splitResult.len gth; i++){
                  tempText = splitResult[i];
                  tempText = tempText.replac e(splitResult[i], '<li>'+splitRes ult[i]+'</li>');
                  textToRemove += tempText;
                  offer += tempText;
                  }

                  offer = offer.replace(t extToRemove,'<u l>'+textToRemov e+'</ul>');
                  alert(offer);
                  }

                  makeUnorderedLi st('abcd\nabcd\ nabcd\nabcd\nab cd\nabcd\n');
                  [/CODE]
                  so the replace works as you have coded it.

                  kind regards

                  Comment

                  • oll3i
                    Contributor
                    • Mar 2007
                    • 679

                    #10
                    well it works when i test it the way You did
                    but it doesnt work when i type some text in the div, select it and then click my button which makes unordered list
                    the seleted text remains in a div and below that text unordered list is created
                    thank You

                    Comment

                    • gits
                      Recognized Expert Moderator Expert
                      • May 2007
                      • 5390

                      #11
                      try to set the innerHTML of the div to an empty string befor setting the new text ...

                      kind regards

                      Comment

                      • oll3i
                        Contributor
                        • Mar 2007
                        • 679

                        #12
                        what i figured out is that the text typed into a div is surrounded. with <p> </p> tags
                        i removed it from the "offer" and now it works
                        thank You
                        Last edited by oll3i; Feb 19 '08, 03:11 PM. Reason: typo

                        Comment

                        • gits
                          Recognized Expert Moderator Expert
                          • May 2007
                          • 5390

                          #13
                          glad to hear you got it working ... post back to the foum anytime you have more questions :)

                          kind regards

                          Comment

                          Working...