-1

Hi all,
I am New in Javascript and Not Having too Much Knowledge of Javascript. I Have a Javascript That Disable Enter Key in Textarea in Whole Form in Page. but I Want to Disable Enter Key only One Textarea.

Below is my Code:

<html>
<head>
<title>The Title Of Your Page Goes Here!</title>
</head>
<body>
<script language="javascript" type="text/javascript">
function checkCR(evt) {
var evt = (evt) ? evt : ((event) ? event : null);
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
if ((evt.keyCode == 13) && (node.type=="text")) {return false;}
}
document.onkeypress = checkCR;
</script>

<p><center><form action="" method="post">
Enter Your Number<br><textarea cols="40" rows="5" name="field1" wrap="physical"></textarea><br>
Enter Your Message<br/><textarea cols="40" rows="5" name="field2" wrap="physical"></textarea>
<input type="submit" value="Send it!"><input type="reset" value="Clear it!" />
</form></center></p>

</body>
</html>

The Above Code Work Fine but This Disable The Enter Key in Twice Textarea but I Want to Disable Enter Key Only in First Textarea That is "field1".

Please Help Me.
Thank You.

3
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by JavaScriptBank
0

I have checked your code.

Actually it does not disable enter key in TextArea. It disable enter key only in Textbox field. But you didn't use any textboxes in your code.


I have changed your code to disable enter key in first text area(field1) only.

<html>
<head>
    <title>The Title Of Your Page Goes Here!</title>
</head>
<body>

    <script language="javascript" type="text/javascript">

        function checkCR(evt) {

            var evt = (evt) ? evt : ((event) ? event : null);
            var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
            if ((evt.keyCode == 13) && (node.type == "textarea") && (node.name == "field1"))
 {
                return false;
            }
        }
        document.onkeypress = checkCR;
    </script>

    <p>
        <center>
            <form action="" method="post">
            Enter Your Number<br>
            <textarea cols="40" rows="5" name="field1" wrap="physical"></textarea><br>
            Enter Your Message<br />
            <textarea cols="40" rows="5" name="field2" wrap="physical"></textarea>
            <input type="submit" value="Send it!">
            <input type="reset" value="Clear it!" />
            </form>
        </center>
    </p>
</body>
</html>

Edited by Ramesh S: n/a

0

Thanx Dear for Solving my Problem.
My Problem is Now Solved..
and am Mark this Thread to Solved..

Thanx once again duse.

-1

another script you can try if need more advanced features :D; it then advances the cursor to the next field within the form. Very useful.

<script type="text/javascript">
/***********************************************
* Disable "Enter" key in Form script- By Nurul Fadilah(nurul@REMOVETHISvolmedia.com)
* This notice must stay intact for use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/
function handleEnter (field, event) {
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if (keyCode == 13) {
			var i;
			for (i = 0; i < field.form.elements.; i++)
				if (field == field.form.elements[i])
					break;
			i = (i + 1) % field.form.elements.length;
			field.form.elements[i].focus();
			return false;
		} 
		else
		return true;
	}      

</script>
	<!--
    	This script downloaded from www.JavaScriptBank.com
    	Come to view and download over 2000+ free javascript at www.JavaScriptBank.com
	-->

Demo: JavaScript Disable 'Enter' Key in Form

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.