Hi,
I am facing the difficulty in the selecting a text on the browser . And highlighting the text.
I am using the window.getSelec tion() to get the object and after that i am using the range.getRangeA t(0).cloneConte nts() method for getting the copy of the document content my requirement is to get the exact html text . But when we are assigning the text into the innerHTML it complete the DOm structure i m not getting the exact HTML text of the document . For example :- if user select the text <i>"why"</i></b> now i m getting the html text as <b><i>"why"</i></b> after applying the below code code. This is the code that i m using :-
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>The serialized HTML of a selection in Mozilla and IE</title>
<script type="text/javascript">
function getHTMLOfSelect ion () {
var range;
if (document.selec tion && document.select ion.createRange ) {
range = document.select ion.createRange ();
return range.htmlText;
}
else if (window.getSele ction) {
var selection = window.getSelec tion();
if (selection.rang eCount > 0) {
range = selection.getRa ngeAt(0);
var clonedSelection = range.cloneCont ents();
var div = document.create Element('div');
div.appendChild (clonedSelectio n);
return div.innerHTML;
}
else {
return '';
}
}
else {
return '';
}
}
</script>
</head>
<body>
<div>
<p>Some text to test the selection on.
Kibology for <b>all</b>. All <i>for</i> Kibology.
</p>
</div>
<form action="">
<p>
<input type="button" value="show HTML of selection"
onclick="this.f orm.output.valu e = getHTMLOfSelect ion();">
</p>
<p>
<textarea name="output" rows="5" cols="80"></textarea>
</p>
</form>
</body>
</html>
Assist me to get rid from this problem.....
Vandit
I am facing the difficulty in the selecting a text on the browser . And highlighting the text.
I am using the window.getSelec tion() to get the object and after that i am using the range.getRangeA t(0).cloneConte nts() method for getting the copy of the document content my requirement is to get the exact html text . But when we are assigning the text into the innerHTML it complete the DOm structure i m not getting the exact HTML text of the document . For example :- if user select the text <i>"why"</i></b> now i m getting the html text as <b><i>"why"</i></b> after applying the below code code. This is the code that i m using :-
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>The serialized HTML of a selection in Mozilla and IE</title>
<script type="text/javascript">
function getHTMLOfSelect ion () {
var range;
if (document.selec tion && document.select ion.createRange ) {
range = document.select ion.createRange ();
return range.htmlText;
}
else if (window.getSele ction) {
var selection = window.getSelec tion();
if (selection.rang eCount > 0) {
range = selection.getRa ngeAt(0);
var clonedSelection = range.cloneCont ents();
var div = document.create Element('div');
div.appendChild (clonedSelectio n);
return div.innerHTML;
}
else {
return '';
}
}
else {
return '';
}
}
</script>
</head>
<body>
<div>
<p>Some text to test the selection on.
Kibology for <b>all</b>. All <i>for</i> Kibology.
</p>
</div>
<form action="">
<p>
<input type="button" value="show HTML of selection"
onclick="this.f orm.output.valu e = getHTMLOfSelect ion();">
</p>
<p>
<textarea name="output" rows="5" cols="80"></textarea>
</p>
</form>
</body>
</html>
Assist me to get rid from this problem.....
Vandit
Comment