How do I remove selected text in javascript?
Thank You
12 3775 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
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
gits 5,390
Expert Mod 4TB
do you already have the selected text retrieved? do you use something like ranges for that? ...
kind regards
-
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
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
yes the replace is not working
function where i try to replace selected text and then make it an unordered list is -
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;
-
}
-
the unordered list is created:) but the selected text from the div with id "editor_displaying_text" is not removed
thank You
gits 5,390
Expert Mod 4TB
hi ...
do you get an error-message? the following example with your code seems to work: -
function makeUnorderedList(txt) {
-
var tempText = "";
-
var textToRemove = "";
-
var splitResult = txt.split('\n');
-
var offer = 'test\nabcd\nabcd\nabcd\nabcd\nabcd\nabcd\ntest1\n';
-
-
offer = offer.replace(txt,'');
-
-
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>');
-
alert(offer);
-
}
-
-
makeUnorderedList('abcd\nabcd\nabcd\nabcd\nabcd\nabcd\n');
-
so the replace works as you have coded it.
kind regards
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
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
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
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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"...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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,...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |