469,626 Members | 1,611 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,626 developers. It's quick & easy.

remove selected text

oll3i
679 512MB
How do I remove selected text in javascript?
Thank You
Feb 18 '08 #1
12 3555
gits
5,390 Expert Mod 4TB
hi ...

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

kind regards
Feb 18 '08 #2
oll3i
679 512MB
i need to remove a text selected in the editable div
offer=offer.replace(txt,'');doesnt work where txt is a passed selected text
i have other function for it
thank You
Feb 18 '08 #3
gits
5,390 Expert Mod 4TB
do you already have the selected text retrieved? do you use something like ranges for that? ...

kind regards
Feb 18 '08 #4
oll3i
679 512MB
Expand|Select|Wrap|Line Numbers
  1.  function getSelectedText(){
  2.  var txt = '';
  3. if (document.getSelection) txt = document.getSelection();
  4. else if (document.selection) txt = document.selection.createRange().text;
  5.  
  6. return txt;
  7.  
  8.  }
  9.  
  10.  
here is my code to get the selected text
i know it works
just the selected text is not replaced with ''
thank You
Feb 18 '08 #5
gits
5,390 Expert Mod 4TB
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
Feb 18 '08 #6
oll3i
679 512MB
yes the replace is not working
function where i try to replace selected text and then make it an unordered list is
Expand|Select|Wrap|Line Numbers
  1.  function makeUnorderedList(txt){
  2.  var tempText="";
  3.  var textToRemove="";    
  4.  var splitResult = txt.split('\n');
  5.  var offer=document.getElementById("editor_displaying_text").innerHTML;
  6.  
  7.  
  8.   offer=offer.replace(txt,''); ,<---this doesnt work
  9.  
  10.  
  11.  
  12.  
  13.  for(i = 0; i < splitResult.length; i++){
  14.     tempText=splitResult[i];
  15.     tempText=tempText.replace(splitResult[i],'<li>'+splitResult[i]+'</li>');
  16.     textToRemove+=tempText;
  17.     offer+=tempText;    
  18. }
  19.  
  20. offer=offer.replace(textToRemove,'<ul>'+textToRemove+'</ul>');
  21. document.getElementById("editor_displaying_text").innerHTML = offer;
  22. }
  23.  
Feb 18 '08 #7
oll3i
679 512MB
the unordered list is created:) but the selected text from the div with id "editor_displaying_text" is not removed
thank You
Feb 18 '08 #8
gits
5,390 Expert Mod 4TB
hi ...

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

Expand|Select|Wrap|Line Numbers
  1. function makeUnorderedList(txt) {
  2.     var tempText     = "";
  3.     var textToRemove = "";    
  4.     var splitResult  = txt.split('\n');
  5.     var offer = 'test\nabcd\nabcd\nabcd\nabcd\nabcd\nabcd\ntest1\n';
  6.  
  7.     offer = offer.replace(txt,'');
  8.  
  9.     for(i = 0; i < splitResult.length; i++){
  10.         tempText = splitResult[i];
  11.         tempText = tempText.replace(splitResult[i], '<li>'+splitResult[i]+'</li>');
  12.         textToRemove += tempText;
  13.         offer += tempText;    
  14.     }
  15.  
  16.     offer = offer.replace(textToRemove,'<ul>'+textToRemove+'</ul>');
  17.     alert(offer);
  18. }
  19.  
  20. makeUnorderedList('abcd\nabcd\nabcd\nabcd\nabcd\nabcd\n');
  21.  
so the replace works as you have coded it.

kind regards
Feb 19 '08 #9
oll3i
679 512MB
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
Feb 19 '08 #10
gits
5,390 Expert Mod 4TB
try to set the innerHTML of the div to an empty string befor setting the new text ...

kind regards
Feb 19 '08 #11
oll3i
679 512MB
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
Feb 19 '08 #12
gits
5,390 Expert Mod 4TB
glad to hear you got it working ... post back to the foum anytime you have more questions :)

kind regards
Feb 19 '08 #13

Post your reply

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

Similar topics

2 posts views Thread by Ramamoorthy Ramasamy | last post: by
3 posts views Thread by Rob | last post: by
8 posts views Thread by rkrishna | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.