View Full Version : How to change characters in a textarea?
Hello,
I'm fairly new to java so please bare with me!
So, at the moment I have a form with a textarea which is generating XHTML for me, this all works fine. Now, I would like to know how to change characters in the textarea when the "generate" button is clicked.
For example!
Say I had a <br> tag... I would like it to loop through the document that has been loaded into the textarea and change all instances to <br />
I would really appreciate it, if someone could push me in the right direction or perhaps provide some code that I can use to achieve this as it's been really bugging me!
Thanks!
waolly
02-16-2005, 04:47 PM
Note it uses a replace function I picked up somewhere which replaces all occurences of a substring within a string.
<html>
<body>
<script>
function replace(string,text,by) {
// Replaces text with by in string
var strLength = string.length, txtLength = text.length;
if ((strLength == 0) || (txtLength == 0)) return string;
var i = string.indexOf(text);
if ((!i) && (text != string.substring(0,txtLength))) return string;
if (i == -1) return string;
var newstr = string.substring(0,i) + by;
if (i+txtLength < strLength)
newstr += replace(string.substring(i+txtLength,strLength),text,by);
return newstr;
}
function generate() {
currenttext=document.getElementById("txt").value;
currenttext=replace(currenttext,"<br>","</ br>");
document.getElementById("txt").value=currenttext;
}
</script>
<textarea id=txt rows=10 cols=50>some text with a <br> and another <br></textarea><br>
<input type=button onclick="generate()" value=generate>
</body>
</html>
You're a star waolly, code worked well with the application I'm building!
I was wondering if this code could also work with the <img> tag.. Although it may not simply because <img> will contain attributes embedded with " ", meaning the closing slash would need to be after the quotation marks.
<img src ="myimage.gif" />
How could I incorporate it so that it will add the trailing slash after any possible quotes. My attempts to do it so far have failed...
Thanks!
waolly
02-17-2005, 11:02 AM
The answer to your problem here lies in regular expressions - which allows for extremely powerful text searching and manipulation.
Here is the code snippet revised with regular expressions - note that this code just changes "img" tags but you can easily extend it.
<html>
<body>
<script>
function generate() {
currenttext=document.getElementById("txt").value;
var r=/(<img.*?[^\/])>/gi;
currenttext=currenttext.replace(r,"$1 />");
document.getElementById("txt").value=currenttext;
}
</script>
<textarea id=txt rows=10 cols=50>some text with a <br> and another <br></textarea><br>
<input type=button onclick="generate()" value=generate>
</body>
</html>
Thanks mate, really appreciate it. I successfully managed to modify the expressions to match the criteria i wanted it to search for.
Once again, many thanks... :)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.