DefKev
10-13-2004, 06:45 PM
This is what i wrote, so far:
<html>
<head>
<script type="text/javascript">
function ReplaceSelection() {
if (window.getSelection) Selection = window.getSelection()
else Selection = document.selection.createRange();
if (Selection == 0) {
alert('No selection made !\nYou have to select something before using this button.');
return; }
else if (Selection.text == 0) {
alert('No selection made !\nYou have to select something before using this button.');
return; }
if (window.getSelection) Replace = prompt('Replace "' + Selection + '" with:\n(leave blank to delete selection)','');
else Replace = prompt('Replace "' + Selection.text + '" with:\n(leave blank to delete selection)','');
if (Replace == null) Replace = Selection;
else if (Replace == null) Replace = Selection.text;
if (window.getSelection) Selection = Replace;
else Selection.text = Replace; }
</script>
</head>
<body>
<p>Text</p>
<p><input value="more Text"></p>
<p><textarea rows="5" cols="20">yet more Text</textarea></p>
<p><input type="button" value="Replace Selection" onclick="ReplaceSelection();"></p>
</body>
</html>
Backround:
Select some text an hit the button, typ in with what you wan't to replace the selection with and press OK - Selection gets replaced (if nothing was selected and you push the button anyway, an alert will pop-up).
Problem:
Works just fine in IE, but i have no idea (at all) how to get the function to replace the selection in gecko (mainly firefox).
I know the selectionStart/-End method but i only got i working inside an </input> and </textarea>, but not with the text inside the document.
Thanks for advice !
Kev
<html>
<head>
<script type="text/javascript">
function ReplaceSelection() {
if (window.getSelection) Selection = window.getSelection()
else Selection = document.selection.createRange();
if (Selection == 0) {
alert('No selection made !\nYou have to select something before using this button.');
return; }
else if (Selection.text == 0) {
alert('No selection made !\nYou have to select something before using this button.');
return; }
if (window.getSelection) Replace = prompt('Replace "' + Selection + '" with:\n(leave blank to delete selection)','');
else Replace = prompt('Replace "' + Selection.text + '" with:\n(leave blank to delete selection)','');
if (Replace == null) Replace = Selection;
else if (Replace == null) Replace = Selection.text;
if (window.getSelection) Selection = Replace;
else Selection.text = Replace; }
</script>
</head>
<body>
<p>Text</p>
<p><input value="more Text"></p>
<p><textarea rows="5" cols="20">yet more Text</textarea></p>
<p><input type="button" value="Replace Selection" onclick="ReplaceSelection();"></p>
</body>
</html>
Backround:
Select some text an hit the button, typ in with what you wan't to replace the selection with and press OK - Selection gets replaced (if nothing was selected and you push the button anyway, an alert will pop-up).
Problem:
Works just fine in IE, but i have no idea (at all) how to get the function to replace the selection in gecko (mainly firefox).
I know the selectionStart/-End method but i only got i working inside an </input> and </textarea>, but not with the text inside the document.
Thanks for advice !
Kev