views:

175

answers:

4

I have a textarea, which will handle output, and a textfield which will handle user input.

Focus will be entirely on the input field. I can't make it so that the user input field will add text when the form is submitted (enter key is pressed). It will only work if there is a button and this is clicked. How do I solve this issue?

Below is the code i'm trying for the enter key submit.

<html>
<head>
<script type="text/javascript">
  function addtxt(input) {
    var obj=document.getElementById(input)
    var txt=document.createTextNode("blah blah")
    obj.appendChild(txt)
  }
</script>
</head>
<body>
<textarea id="textarea1"></textarea>
<br><input type="text" onSubmit="addtxt('textarea1');">
</body>
</html>
A: 

Use the form element

<form onsubmit="addtxt('textarea1')">
    <textarea id="textarea1"></textarea>
    <br><input type="text" />
</form>
BGerrissen
OK, this worked. However, after the text is written to the textarea, the page is immediately refreshed and the text disappears.
Big Mo Fo
Ah then you dont want to submit at all
BGerrissen
+1  A: 

Instead of submit, try using the keypress event. Detect when the enter key is pressed, copy the data, and cancel the event (to prevent form submission). If you allow the form to submit, it will simply replace the existing page with the result of the form post.

Modifying your current code:

<html> 
<head> 
<script type="text/javascript"> 
  function addtxt(e,ctl,input) {
    var key;
    if (window.event) {
       key = event.keyCode;
    } else {
       key = e.which;
    }
    if (key == 13) {
       var obj=document.getElementById(input);
       var txt=document.createTextNode("blah blah");
       obj.appendChild(txt);
       ctl.value = '';
       return false;
    }
    return true;
  } 
</script> 
</head> 
<body> 
<textarea id="textarea1"></textarea> 
<br><input type="text" onkeypress="return addtxt(event,this,'textarea1');"> 
</body> 
</html>

Note that there may be much better ways to achieve your ultimate goal, but since you don't state what that is, this is really the best I can do. Also, I'd would definitely look at using a framework like jQuery/Dojo/Prototype and add the handlers unobtrusively.

tvanfosson
This sounds like a good method... but how would I go about achieving it?
Big Mo Fo
@Big - I added an example.
tvanfosson
Thankyou, this is good! Basically, what i'm trying to achieve is a rudimentary terminal-like-thing. I want to be able to type something into the input box, press enter, have the script read it, run the input through an if statement and output something based on what was entered.
Big Mo Fo
However, this code won't accept any other keypress...
Big Mo Fo
@Big -- I'll fix it. Don't know what I was thinking. It should return true unless it's an enter. Presumably the input box should be cleared as well.
tvanfosson
Awesome tvan, this is really cool. Helping me learn a little. What does the ctl.value = '' do? This syntax really reminds me a lot of actionscript...
Big Mo Fo
Some points: using the `keydown` event will mean you can use the `keyCode` event property in all browsers. Also, `return true` is redundant: returning `undefined` (which is what happens in the absence of a `return` statement) does what you want. Finally, see my answer for details, but manipulating a textarea's value should be done via the `value` property.
Tim Down
Do any of y'all know how I can modify this code to make each statement inserted onto a new line in the textarea? And the big question, I guess: would be where would I insert the if statement to read the user's input and output some text in the textarea based on what's read?
Big Mo Fo
A: 

You can use JQuery

$('textarea#textarea1').keypress(function(e) {
    if (e.keyCode == 13) { // enter
       //do some stuff
    }
});
jcubic
A: 

This will do the job. Also, you should deal with the value property of the textarea rather than appending text nodes to it: if the user changes the textarea's value at all, changing its child nodes afterwards will have no effect. If you want the textarea to be read-only, add a readonly attribute: <textarea id="textarea1" readonly></textarea>.

<script type="text/javascript">
    function inputKeyDown(evt, input) {
        if (evt.keyCode == 13) {
            var textarea = document.getElementById("textarea1");
            textarea.value += "\n" + input.value;
            input.value = ""; // I'm guessing you may want this
            return false;
        }
    }
</script>

<input type="text" onkeydown="return inputKeyDown(event, this);">
Tim Down
I completely overlooked this reply, Tim, but this is EXACTLY the kind of foundation I want. After toggling with the other answer for a couple of hours, I finally noticed this post and plugged it in. You are a javascript god. Can anyone recommend any good tutorials? I have a little knowledge of python and working knowledge of both CSS and xHtml.
Big Mo Fo