Help - Search - Members - Calendar
Full Version: Set Var To File Or Textarea
Zymic Webmaster Forums > Web Design & Development > Client Side Scripting > JavaScript
Symasta
Hey
I bet someone can help me here.
I am using javascript and I am trying to set a variable equal to a textarea, so that I can use whatever I put in the textarea and replace parts of it after a certain way.
My code (that does not work)

<html>
<head>

&lt;script type="text/javascript">


function replace()
{
var txt=document.getElementsByTagName("*what goes in here???*");
alert(txt);
document.write(txt.replace(/something/g, "somethingelse"));
}


</script>
</head>
<body>
<form id="textarea" onsubmit="replace()">
<textarea rows="10" cols="30">
TEXTTEXTTEXT
</textarea>
<input type="submit" id="submit" value="Submit" />
</form>

</body>
</html>

----------------------------------------------------------------------------------

The other problem I have:
I am trying to execute a javascript function, and this function should include a .php text file on the current page.
(So that it would work like a php include)
But any way I try, my javascript code does not work.
Any suggestions?

Thanks for your help


Symasta
Ed
You're using 'getElementsByTagName' this is for pulling an array of all nodes with that tag name, pretty sure you're looking to use 'getElementById', your replace function would simply be:

CODE
function replaceText()
{
   var txt = document.getElementById('textarea').value;
   document.write('txt.replace(/something/g, 'somethingelse');
}


In regards to retrieving a remote file for inclusion, have a look into XHR (also known as AJAX), I have written a library called simplejax (it's the javascript one, not the php one!) and this would make it a doddle:
http://simplejax.sourceforge.net

This example should help with what you're trying:
http://simplejax.sourceforge.net/#dynamic

If you find it's cached, append a timestamp to the end, like here:
http://simplejax.sourceforge.net/#argument

Hope that helps.
Symasta
Thanks alot!
The first one worked.
Had to set the id of the actual textarea to textarea and then it worked perfectly fine.
Have another two questions though.
I am trying to reshape a forum text, so I will have more than one replacement, BUT I do not want to write it again every single time.
That is what a
document.write(txt.replace(/something2/g, 'somethingelse'));
for every new replacement would do.
how do I add replacements. With an array or how?
and how do I do replacements if I have some words that are always the same and some that vary so
Name X o'clock at X(date)
and I have a vary of 35 names which I could put in, but the rest is time and date, and I want to replace it with another code...how do I do that?

Your sides looked like they are what I probably need, I just don't have the time right now to check it.

Thanks alot

Symasta
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.