...

View Full Version : Need help in function!



newbee_coder
01-25-2009, 12:59 AM
Hello,

I am writing code for my form which has 2 textbox One is labelled as "search" and the other one is "replace" and one textarea. If a user enters "hello how are you" in the textarea and user needs to change that hello to hi, he/she needs to enter "hello" in search textbox and "hi" in "replace" textbox. There is another button that says "replace now", if user clicks that button hello changes to hi.

Can anyone give me some hints please. I am so confused how can I define such function?

Thank you,

demtron
01-25-2009, 01:56 AM
I assume that you want to do this in JavaScript. You will need to be familiar with the following methods and concepts:

- string.replace()
- regular expression syntax
- document.getElementById()
- object.click()

Here are some resources for finding the answers to these questions:

w3schools.com
http://msdn.microsoft.com/en-us/library/default.aspx (look under 1) Web Development -> HTML and CSS and 2) Web Development -> Scripting

Hope that helps!

Philip M
01-25-2009, 08:42 AM
Try this:-


<textarea id = "txtarea" rows = "12" cols = "40"></textarea><br><br>
Search For <input type = "text" name = "search" id = "search" size = "20"><br>
Replace By <input type = "text" name = "rep" id = "rep" size = "20"><br>
<input type = "button" name = "but1" value = "Replace Now" onclick = "doIt()">


<script type = "text/javascript">
function doIt() {
var txt = document.getElementById("txtarea").value;
var str2rep = document.getElementById("search").value;
var repstr = document.getElementById("rep").value;
var len = txt.length;
var a = txt.indexOf(str2rep);
if (a < 0) {
alert ("This sequence of characters was not found ");
}
else {
var newStr = txt.substr(0,a);
newStr = newStr + repstr;
var x = Number(a + str2rep.length);
newStr = newStr + txt.substring(x,len);
document.getElementById("txtarea").innerHTML=newStr;
}
}

</script>

Note that the search is case-sensitive. Hello and hello are not the same. If the same character sequence occurs in the textarea twice you can replace the second occurence by clicking on the button a second time. Or if you want one click to replace all the searched for character sequences at once ask me for a revised script.



“In ancient times they had no statistics, so they had to fall back on lies” — Stephen B Leacock

newbee_coder
01-27-2009, 01:58 AM
Thank you so much Philip!
I got an error when I run the code. It is saying...
Error: document.getElementById("search") is null
Source File: file:///E:/USB/*************

I think the below line has a bug in it.

var str2rep = document.getElementById("search").value;

Thanks,

Philip M
01-27-2009, 09:40 AM
Well, it works fine for me.

Are you quite sure that you have copied it correctly?

newbee_coder
01-27-2009, 09:42 AM
yes! I am sure!!

Thanks,

Philip M
01-27-2009, 09:52 AM
As I say, it works fine for me and there is no error in the code.

What, exactly, are you entering in the Search For box?
And in the Replace By box?

newbee_coder
01-27-2009, 09:58 AM
if you dont mind, can I PM you my entire code?

Thanks,

Philip M
01-27-2009, 10:37 AM
if you dont mind, can I PM you my entire code?

Thanks,

I seem to have got it whether I mind or not.

The code as PM'd to me works perfectly for me in IE6. Are you using some ancient browser? Or using an inappropriate DOCTYPE? Otherwise I am right out of ideas.
Also tested in Firefox and it works fine - there is absolutely no reason why it should not work.

rangana
01-27-2009, 11:53 AM
Try to change highlighted with value instead:


document.getElementById("txtarea").innerHTML=newStr;

Philip M
01-27-2009, 12:36 PM
Try to change highlighted with value instead:


document.getElementById("txtarea").innerHTML=newStr;


No, that is not the problem OP says he gets error message at
Error: document.getElementById("search") is null

But you can change innerHTML to value if desired - it makes no difference. As I say, it works for me in IE and Firefox.

rangana
01-27-2009, 12:53 PM
Hi Philip,

Using innerHTML, I get the desired results on IE7, but not on FF 3.0.5.

Using value, I get the desired results on both browsers.

...but none of them throws the error that the OP said:
Error: document.getElementById("search") is null

Reading that, I suppose we both assumed that there's no element having the id of search (yet).

Anyway, you might want to share to us the OP's code as passed via PM.

Philip M
01-27-2009, 01:22 PM
[QUOTE=rangana;774766]Hi Philip,

Using innerHTML, I get the desired results on IE7, but not on FF 3.0.5.

Using value, I get the desired results on both browsers.

[QUOTE]

Hmm. Works for me with innerHTML in both IE6 and Firefox 3.0.5 But I do agree that value is better.

Code is as supplied in Post#3

rangana
01-27-2009, 02:52 PM
Let's hear it from the OP, whether changing to value have make/break the script.

I've tried a lower version, FF 3.0.3, and still failing at my end using innerHTML.

Anyway, let's just hear it from the OP.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum