• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5905
  • Last Modified:

JavaScript clear textarea when enter key is pressed - still leaves return character

<html>
<head>
<title> just testing </title>
<script language="JavaScript">
<!--
function send_iswriting(e){
      var key = -1 ;
      var shift ;

      key = e.keyCode ;
      shift = e.shiftKey ;

      if ( !shift && ( key == 13 ) )
      {
            document.form.reset() ;
      }
}
//-->
</script>
</head>
<body>
<form name="form">
<textarea name="message" cols=40 rows=5 onKeyPress="return send_iswriting(event);" wrap="virtual"></textarea>
<input type="reset" value="reset" OnClick="document.form.message.focus()">
</form>
</body>
</html>

===

When you press ENTER, it should clear the form... it doesn't, it leaves a blank character behind.  If you were to "click" ont he reset button, it works... why doesn't it clear when you hit ENTER?  How can this be fixed?

Thank you,

Nate
0
nskate
Asked:
nskate
  • 6
  • 5
  • 2
  • +1
1 Solution
 
archrajanCommented:
the blank character is due to the enter key and it cannot be taken off
0
 
nskateAuthor Commented:
There HAS to be a solution... Isn't there?  Is there a  work around without reloading the page?  I find this hard to believe.
0
 
devicCommented:
try onKeyUp
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
GwynforWebCommented:
Use keyup not keypress, (the enter is recorded after the key press event)

<head>
<title> just testing </title>
<script language="JavaScript">
<!--
function send_iswriting(e){
     var key = -1 ;
     var shift ;

     key = e.keyCode ;
     shift = e.shiftKey ;

     if ( !shift && ( key == 13 ) )
     {
          document.form.reset() ;
     }
}
//-->
</script>
</head>

<body>

<form name="form">
  <p><textarea name="message" cols="40" rows="5" onKeyup="return send_iswriting(event);"
  wrap="virtual"></textarea> <input type="reset" value="reset"
  OnClick="document.form.message.focus()"> </p>
</form>
</body>
</html>
0
 
GwynforWebCommented:
and even simpler solution is this

<form name="form">
 <textarea name="message" cols="40" rows="5" onKeyup=" this.value=this.value.replace(/[\n\r]*/g,'')"  wrap="virtual">
 </textarea>
<input type="reset" value="reset" OnClick="document.form.message.focus()"> </p>
</form>
0
 
GwynforWebCommented:
I mean this, sorry. The above stops line breaks if that is what you really want

<form name="form">
 <textarea name="message" cols="40" rows="5" onKeyup=" this.value=''"  wrap="virtual">
 </textarea>
<input type="reset" value="reset" OnClick="document.form.message.focus()"> </p>
</form>
0
 
devicCommented:
Gwyn, very useful form :)
0
 
GwynforWebCommented:
:-)
0
 
archrajanCommented:
great!!! hats off Gwyn
0
 
devicCommented:
again Gwyn posts comment with answer from my previous post and takes all the points...

nice work ;)
0
 
GwynforWebCommented:
nskate, Thanks for the points

devic , I posted to a unrefreshed page and my answer arrived 2 mins after you, if I had not tested my code and taken time to expain in detail what was happeing I would have posted 3 or 4 mins before you.
0
 
devicCommented:
>>> I posted to a unrefreshed page and my answer arrived 2 mins after you
>>>if I had not tested my code and taken time to expain in detail what was happeing I would have posted 3 or 4 mins before you.

and then you posted other two comments, after your "tested code"....
why? not enough tested onkeyup event?

Or maybe you saw that I was first with right answer, and you began to do something else to take attention of the asker....

and now, after all, instead of what an expert should say in this situation you say again bullshit.

shame on you!




0
 
GwynforWebCommented:
I like giving value for money by giving more info about other ideas that may be possible after answering the question as stated (with testd code). 2 word answers are not my style and don't usually get the points.  Sour grapes do not good on you.
0
 
devicCommented:
>>>2 word answers are not my style and don't usually get the points.

who says this??? you??? hahaha :)

look here: http:Q_21194923.html#12497360


The greatest of faults I should say, is to be conscious of none.
Carlyle
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now