How can I find and select "String" in iframe and then replace it by another string?,
The following codes are working perfectly but I could not combine them together,
function Find(txt) {
var iframe = document.getElementById("Text_ifr");
var iframe_contents = iframe.contentDocument.body.innerHTML;
if (iframe_contents.indexOf(txt) >= 0) {
alert('Found')
}
else { alert('Not Found') }
return false;
}
function replaceSelectedText(replacementText) {
var iframe = document.getElementById("Text_ifr");
var win = iframe.contentWindow;
var doc = iframe.contentDocument || win.document;
var sel, range;
if (win.getSelection) {
sel = win.getSelection();
if (sel.rangeCount) {
range = sel.getRangeAt(0);
range.deleteContents();
range.insertNode(doc.createTextNode(replacementText));
}
} else if (doc.selection && doc.selection.createRange) {
range = doc.selection.createRange();
range.text = replacementText;
}
}
Thanks
A scientist shouldn’t be asked to judge the economic and moral value of his work. All we should ask the scientist to do is find the truth and then not keep it from anyone
“Arthur Kornberg”
The following codes are working perfectly but I could not combine them together,
Instead of combining both function together. You can try calling one replaceSelectedText method from inside the Find function like below
function Find(txt) {
var iframe = document.getElementById("Text_ifr");
var iframe_contents = iframe.contentDocument.body.innerHTML;
if (iframe_contents.indexOf(txt) >= 0) {
//Here you can call your function to replace the text
replaceSelectedText(txt);
alert('Found');
}
else { alert('Not Found') }
return false;
}
Thanks for the prompt response, this works only if I select the text manually, How can I force auto selection and replacement?
Thanks
A scientist shouldn’t be asked to judge the economic and moral value of his work. All we should ask the scientist to do is find the truth and then not keep it from anyone
“Arthur Kornberg”
yes, I am passing values using query string to a template RTF, The template has some keywords that I want to find and replace them with values from query string
ie, in the iframe innerhtml doc I have "Partner1", I want to locate it and replacement with another text
I Managed to find the text but i could not replace it without manual selection
A scientist shouldn’t be asked to judge the economic and moral value of his work. All we should ask the scientist to do is find the truth and then not keep it from anyone
“Arthur Kornberg”
This wont work, allow me please to rephrase my question, let me say that I have a textbox and "Microsoft SQL Server" word in it, how can I find SQL and select it automatically?
it is more or less like "Find and Replace" in MS office
Thanks
A scientist shouldn’t be asked to judge the economic and moral value of his work. All we should ask the scientist to do is find the truth and then not keep it from anyone
“Arthur Kornberg”
<script type="text/javascript">
function Find(txt, replacement) {
var iframe = document.getElementById("Text_ifr");
var iframe_contents = iframe.contentDocument.body.innerHTML;
if (iframe_contents.indexOf(txt) >= 0) {
alert('Found')
iframe.contentDocument.body.innerHTML = iframe.contentDocument.body.innerHTML.replace(txt, replacement);
}
else { alert('Not Found') }
return false;
}
A scientist shouldn’t be asked to judge the economic and moral value of his work. All we should ask the scientist to do is find the truth and then not keep it from anyone
“Arthur Kornberg”
A scientist shouldn’t be asked to judge the economic and moral value of his work. All we should ask the scientist to do is find the truth and then not keep it from anyone
“Arthur Kornberg”
Member
107 Points
222 Posts
Find, Select and Replace!
Nov 30, 2015 03:20 AM|Embryologist|LINK
Hi,
How can I find and select "String" in iframe and then replace it by another string?,
The following codes are working perfectly but I could not combine them together,
Thanks
“Arthur Kornberg”
All-Star
50021 Points
9684 Posts
Re: Find, Select and Replace!
Nov 30, 2015 04:42 AM|A2H|LINK
Instead of combining both function together. You can try calling one replaceSelectedText method from inside the Find function like below
A2H
My Blog | Dotnet Funda
Member
107 Points
222 Posts
Re: Find, Select and Replace!
Nov 30, 2015 04:45 AM|Embryologist|LINK
Thanks for the prompt response, this works only if I select the text manually, How can I force auto selection and replacement?
Thanks
“Arthur Kornberg”
Contributor
3839 Points
1384 Posts
Re: Find, Select and Replace!
Nov 30, 2015 05:13 AM|vahid bakkhi|LINK
hi
could you please more details,
your mean was when your page loaded find the text or other things
Please MARK AS ANSWER if suggestion helps.
Member
107 Points
222 Posts
Re: Find, Select and Replace!
Nov 30, 2015 05:16 AM|Embryologist|LINK
yes, I am passing values using query string to a template RTF, The template has some keywords that I want to find and replace them with values from query string
ie, in the iframe innerhtml doc I have "Partner1", I want to locate it and replacement with another text
I Managed to find the text but i could not replace it without manual selection
“Arthur Kornberg”
Contributor
3839 Points
1384 Posts
Re: Find, Select and Replace!
Nov 30, 2015 05:44 AM|vahid bakkhi|LINK
you can when your iframe load be completed, call your java-script function , please below code:
Please MARK AS ANSWER if suggestion helps.
Member
107 Points
222 Posts
Re: Find, Select and Replace!
Nov 30, 2015 06:44 AM|Embryologist|LINK
Dear,
This wont work, allow me please to rephrase my question, let me say that I have a textbox and "Microsoft SQL Server" word in it, how can I find SQL and select it automatically?
it is more or less like "Find and Replace" in MS office
Thanks
“Arthur Kornberg”
Member
107 Points
222 Posts
Re: Find, Select and Replace!
Nov 30, 2015 07:16 AM|Embryologist|LINK
I finally Found it,
Thanks guys for your input
“Arthur Kornberg”
Contributor
3839 Points
1384 Posts
Re: Find, Select and Replace!
Nov 30, 2015 07:21 AM|vahid bakkhi|LINK
here i put a full sample that you find your word and replace to other things :
i hope it can be helpful
Please MARK AS ANSWER if suggestion helps.
Member
107 Points
222 Posts
Re: Find, Select and Replace!
Nov 30, 2015 07:24 AM|Embryologist|LINK
Thanks, Much appreciated
“Arthur Kornberg”