Automatically replace a character when typing

Results 1 to 3 of 3

Thread: Automatically replace a character when typing

  1. #1
    Linda Guest

    Default Automatically replace a character when typing

    If I want to automatically have a character replaced when typed, how would I do this??? I.E. I want &#039 to be replaced with `. I have tried a few things but what ends up happening is ` is being typed and I&#039m not allowed to enter anything else in the field. I am probably calling my function wrong and my function is wrong all together. This is what I used:::::<BR>function textareacheck()<BR>{if(document.forms[0].comments.value="&#039") <BR> document.forms[0].comments.value="`" <BR> return true;}<BR><BR>I call the function like this:::: <BR>&#060;textarea name=comments cols=46 rows=6 onkeypress="textareacheck();"&#062; &#060;/textarea&#062;<BR>

  2. #2
    SteveC Guest

    Default RE: Automatically replace a character when typing

    A few syntax errors:<BR><BR>Your if statement needs squiggly brackets, unless it&#039s on the same line. Also, equality in JavaScript is ==, not the assignement operator =. So you want to use:<BR><BR>function textareacheck(){<BR> if(document.forms(0).comments.value == "&#039") {<BR> document.forms(0).comments.value = "`";<BR> }<BR>return true;<BR>}<BR><BR>However, you are checking the entire value of the text area... not a good thing to do, unless you know there will be only one character in the textarea. If that&#039s the case, the value will never equal just &#039. What I would do is when the the textarea loses focus, place the value into a string, and do a search and replace.<BR><BR>Secondly, this syntax will work in IE, but Netscape will throw up on it. You need to pass the form into your functions.

  3. #3
    Join Date
    Dec 1969

    Default Tough to do cross-browser...

    Doing on-the-fly replacement is not easy. And it requires code that is aware of which browser is in use, because NS and MSIE support the ability in different ways. And there are some browsers that don&#039t really support it right, at all.<BR><BR>Why do you want to do it on the fly?<BR><BR>Why don&#039t you just wait until you get the text into your next ASP page and then do it in one single Replace call there?<BR><BR>If this is for putting the text into a DB using SQL, the RIGHT solution is to simply do<BR><BR>&#060;%<BR>theText = Replace( Request.Form("fieldOnForm"), "&#039", "&#039&#039" )<BR>SQL = "INSERT INTO table (textField) VALUES (&#039" & theText & "&#039);"<BR>%&#062;<BR><BR>But if you really want to do the replace-on-the-fly, then go to and the JavaScript FAQ and I think you can find or figure out the solution.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts