/    Sign up×
Community /Pin to ProfileBookmark

Is a Noob doing this right?

Is this code ok or, is there a better way to do it?
I’m sure there is a better way to do it, I have this snippet in my editor and it almost freezes on big pages. ?

[code=php]

<HTML>
<HEAD><TITLE> replace </TITLE>

<SCRIPT type=”text/javascript”>
function ReplaceText(HH,ZZ){
var txt=document.getElementById(‘txtArea’).value.length;
for(i=0;i<txt;i++){
document.getElementById(‘txtArea’).value=document.getElementById(‘txtArea’).value.replace(HH,ZZ);
}
}

</SCRIPT>
</HEAD>
<BODY>

<input id=”myBad” value=”H” size=”2″>
<input ID=”RPbut1″ type=”button” value=”Replace” onClick=”ReplaceText(document.getElementById(‘myBad’).value,document.getElementById(‘myExcuse’).value)”>
<input id=”myExcuse” value=”Z” size=”2″>
<input id=”RPbut2″ type=”button” value=”Undo” onClick=”ReplaceText(document.getElementById(‘myExcuse’).value,document.getElementById(‘myBad’).value)”>
<br>
<textarea id=”txtArea”>Howdy Here how are Hwo?</textarea>

</BODY></HTML>
[/code]

Maybe doing it with a while loop would be better?
I really don’t know what I’m doing. I grabbed this idea from someone last week.

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@KorJan 18.2006 — What do you want to do, in fact? Say in words... ?
Copy linkTweet thisAlerts:
@Sup3rkirbyJan 18.2006 — Someone please correct me if i'm wrong, but this should work.

[CODE]
<HTML>
<HEAD>
<TITLE>replace</TITLE>
<SCRIPT type="text/javascript">
function ReplaceText(HH,ZZ)
{
txtArea.value = txtArea.value.replace(/HH/g,ZZ);
}

</SCRIPT>
</HEAD>
<BODY>

<input id="myBad" value="H" size="2">
<input ID="RPbut1" type="button" value="Replace" onClick="myBad.value,myExcuse.value)">
<input id="myExcuse" value="Z" size="2">
<input id="RPbut2" type="button" value="Undo" onClick="ReplaceText(myExcuse.value,myBad.value)">
<br>
<textarea id="txtArea">Howdy Here how are Hwo?</textarea>

</BODY>
</HTML>
[/CODE]

I'm pretty sure that will work.
Copy linkTweet thisAlerts:
@CrazyMerlinJan 18.2006 — yes it could be better.

for single characters it would work, but what if you/user wanted to replace multiple characters? You would end up iterating way too many times.

try this:
[CODE]
function replaceText(HH,ZZ){
while(document.getElementById('txtArea').value.indexOf(HH)!=-1){
document.getElementById('txtArea').value = document.getElementById('txtArea').value.replace(HH,ZZ);
}
}
[/CODE]
Copy linkTweet thisAlerts:
@jzwpauthorJan 18.2006 — Sup3rkirby , yeah sure, after some tweeking it works but there has to be a loop to do the entire length of txtArea else, I have to keep pushing the button.

A little more help please? ?
Copy linkTweet thisAlerts:
@jzwpauthorJan 18.2006 — I will have to study what indexOf means tho...

[code=php]
<HTML>
<HEAD>
<TITLE>replace</TITLE>
<SCRIPT type="text/javascript">

function ReplaceText(HH,ZZ){
while(document.getElementById('txtArea').value.indexOf(HH)!=-1){
txtArea.value = txtArea.value.replace(HH,ZZ);}
}
</SCRIPT>
</HEAD>
<BODY>

<input id="myBad" value="Howdy" size="6">
<input ID="RPbut1" type="button" value="Replace" onClick="ReplaceText(myBad.value,myExcuse.value)">
<input id="myExcuse" value="Z" size="2">
<input id="RPbut2" type="button" value="Undo" onClick="ReplaceText(myExcuse.value,myBad.value)">
<br>
<textarea id="txtArea">Howdy Here how are Hwo?</textarea>

</BODY>
</HTML>
[/code]
Copy linkTweet thisAlerts:
@Sup3rkirbyJan 18.2006 — That looks like it will work(and it does, right).

I believe(i am just guessing), that indexOf means the position of the string(or character) you are trying to replace. Like if you have "This is a string" and you want to replace 'is' then the index will be 3(3rd character is where it starts). and the next index would be 6. but if the character or string is not in the text, then it will be an index of -1. so that is like loop that says as long as the index of the string you want to replace is not -1(meaning it is in the text), then replace it).


*sorry for that sloppy explaination, but maybe it helped*
Copy linkTweet thisAlerts:
@jzwpauthorJan 18.2006 — http://www.w3schools.com/jsref/jsref_obj_string.asp

I really like the way W3 has put their pages together.

Can I download the whole package somhow?
Copy linkTweet thisAlerts:
@CrazyMerlinJan 18.2006 — if you go into IE, you can download their entire site.

just set it up as an offline site.

then set up synchronization to update the pages, say once per week

you just need to set levels.

oh yeah, it's asp, you may need to set yourself up a server too. lol
×

Success!

Help @jzwp spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...