473,378 Members | 1,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

remove selected text

oll3i
679 512MB
How do I remove selected text in javascript?
Thank You
Feb 18 '08 #1
12 3774
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

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

Similar topics

2
by: Ramamoorthy Ramasamy | last post by:
Hi all, I would like to create a page with VBscript and ASP which will contain two list-boxes one in the left side and the other in the right side with two buttons namely "Add>>" and "<<Remove"...
3
by: Rob | last post by:
Hi, I've got a small javascript problem and I'm kinda stuck. I'm using classic ASP. I have a select box which is populated by a database query and there is a buttom that when clicked it will move...
8
by: rkrishna | last post by:
I have an issue that I am trying to solve. I have three select lists (supposed to be preference 1, 2,3) and each of them have the same three options. As soon as I select one of the choices from...
1
by: Dan Bass | last post by:
I'm looking to develop a listbox with in-place editing where as each item is selected, it grows to fit in all the text boxes. When the item is deselected, it shrinks back to its original size. The...
4
by: Ron | last post by:
I've got a listbox that holds a list of groups. Users can select a group, hit the remove button and the group should be removed from the listbox. The only problem is that no matter which group you...
0
by: deepak | last post by:
i have set multiple selection property in bith listboxes(html control) to true. i have taken 2 buttons(html control) say Button1,Button2.now i want to add mutiple selected items to another listbox...
0
by: a | last post by:
I'm trying to delete an item from a collection, by clicking on a delete button in a GridView control. The item in the collection is stored in the Profile object (serialized as xml in the...
7
by: Mike Johnson | last post by:
How do I remove a item that's been selected from a listview? I'm using Visual Basic 2005 express edition. Thanks
7
chunk1978
by: chunk1978 | last post by:
hello. so i have 2 select menus which add and remove options from a 3rd select menu... it seems, however, that it's not possible to use different select menus to toggle a 3rd, because when an...
6
by: Romulo NF | last post by:
Greetings again to everyone, Im back to show this grid componenet i´ve developed. With this grid you can show the data like a normal table, remove the rows that you need, add rows, import data,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.