469,327 Members | 1,192 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,327 developers. It's quick & easy.

Javascript Adding Spaces Question

Does anyone know how I might add a function to this script that continually adds one space before and after the entire query (not in between every word though)?

Example:

If I type in the textarea: "hello there", before the Change button is clicked the string is " hello there ", and then it is parsed as such?


[HTML]<html>

<head>

<script type = "text/javascript">

function replaceChars() {
var temp = document.subform.text.value;


temp = temp.replace(/hello there/gi, "goodbye there");
temp = temp.replace (/this/gi, "that");
temp = temp.replace (/today/gi, "tomorrow");


document.subform.text.value = temp;
}

</script>

</head>

<body>


<form name="subform">

<textarea rows="8" name="text" cols="31" value=""></textarea>
<br>
<input type=button name=action value="Change" onClick="replaceChars(document.subform.text.value) ;">


</form>
</body>

</html>

[/HTML]
Thanks!
Aug 17 '07 #1
1 1483
acoder
16,027 Expert Mod 8TB
Please use code tags for posting code. Thanks!

Just add a space to beginning and end of the textarea value:
Expand|Select|Wrap|Line Numbers
  1. " " + document...value + " "
Aug 17 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

By using this site, you agree to our Privacy Policy and Terms of Use.