473,394 Members | 1,693 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,394 software developers and data experts.

Selection with HTML?

Hi,

This question has been asked a few times in this group, but I
haven't been able to find an answer to any of the posts. window.getSelection()
returns the selected pure text, how to get it with the HTML tags?

Thanks a lot for any help.

--
Bruno Barberi Gnecco <brunobg_at_users.sourceforge.net>
Simon's Law:
Everything put together falls apart sooner or later.
Jun 1 '07 #1
2 1573
Bruno Barberi Gnecco wrote:
This question has been asked a few times in this group, but I
haven't been able to find an answer to any of the posts.
window.getSelection()
returns the selected pure text, how to get it with the HTML tags?
window.getSelection() gives you a selection object, not a string. You
can access a range in the selection and clone its contents, then append
to a div to extract the innerHTML e.g.
var selection = window.getSelection();
var range = selection.getRangeAt(0);
if (range != null) {
var div = document.createElement('div');
div.appendChild(range.cloneContents());
alert(div.innerHTML);
}

<http://www.xulplanet.com/references/objref/Selection.html>
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jun 1 '07 #2
Martin Honnen wrote:
Bruno Barberi Gnecco wrote:
> This question has been asked a few times in this group, but I
haven't been able to find an answer to any of the posts.
window.getSelection()
returns the selected pure text, how to get it with the HTML tags?


window.getSelection() gives you a selection object, not a string. You
can access a range in the selection and clone its contents, then append
to a div to extract the innerHTML e.g.
var selection = window.getSelection();
var range = selection.getRangeAt(0);
if (range != null) {
var div = document.createElement('div');
div.appendChild(range.cloneContents());
alert(div.innerHTML);
}

<http://www.xulplanet.com/references/objref/Selection.html>

Thanks a lot, exactly what I needed.

--
Bruno Barberi Gnecco <brunobg_at_users.sourceforge.net>
It is exactly because a man cannot do a
thing that he is a proper judge of it.
-- Oscar Wilde
Jun 1 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jean Biver | last post by:
Dear readers: I found a web page with 2 selection lists in a form. The contents of the second selection list are updated based on the entry in the first selection list. For example: if the...
8
by: lawrence | last post by:
I'm a beginner with Javascript and especially cross-browser Javascript. I got this working in IE, but not in Netscape 7. It seems like, in Netscape, every time I click on a button, the focus shifts...
5
by: STeve | last post by:
Hey guys, I currently have a 100 page word document filled with various "articles". These articles are delimited by the Style of the text (IE. Heading 1 for the various titles) These articles...
11
by: Xah Lee | last post by:
Of interest: • The Semicolon Wars, by Brian Hayes. 2006. http://www.americanscientist.org/template/AssetDetail/assetid/51982 in conjunction to this article, i recommend: • Software...
3
kendall
by: kendall | last post by:
I'm currently working on a PHP CMS for my school that uses simple forms to update the pages and database. To get it out in use, it will have to be usable by people who don't know that is bold, and...
4
by: sialater | last post by:
Hello, I realise there are a lot of topics related to this problem but many of what I have found has run cold or unresolved. What I have is an addressbook clone where there are groups which have...
7
by: robtyketto | last post by:
Greetings, I'm slowly building up code to do the following:- Display TWO selection option boxes (cascading). If the FIRST selection option box changes then reload the jsp using onchange...
2
by: vanditnagar | last post by:
Hi, I am facing the difficulty in the selecting a text on the browser . And highlighting the text. I am using the window.getSelection() to get the object and after...
3
by: Venturini | last post by:
I am trying to put together a web page where the customer makes choices of products and is then given a total. I am extremely new to Javascript and have managed to get as far as I have from web...
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: 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...
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
0
BarryA
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...
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...
0
marktang
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.