By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,058 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,058 IT Pros & Developers. It's quick & easy.

remove selected text

oll3i
100+
P: 679
How do I remove selected text in javascript?
Thank You
Feb 18 '08 #1
Share this Question
Share on Google+
12 Replies


gits
Expert Mod 5K+
P: 5,371
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
100+
P: 679
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
Expert Mod 5K+
P: 5,371
do you already have the selected text retrieved? do you use something like ranges for that? ...

kind regards
Feb 18 '08 #4

oll3i
100+
P: 679
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
Expert Mod 5K+
P: 5,371
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
100+
P: 679
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
100+
P: 679
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
Expert Mod 5K+
P: 5,371
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
100+
P: 679
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
Expert Mod 5K+
P: 5,371
try to set the innerHTML of the div to an empty string befor setting the new text ...

kind regards
Feb 19 '08 #11

oll3i
100+
P: 679
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
Expert Mod 5K+
P: 5,371
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.