Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

prevent submit form in textarea when enter key used

Status
Not open for further replies.

Tracey

Programmer
Oct 16, 2000
690
NZ
Hi there

I have a form with various inputs (txt, checkbox, textarea, submits). My problem revolves around the textarea. If a user wants to add a double line between paragraphs by hitting the enter key twice, the form is submitted on the second enter. (this behaviour is present in both IE7 and Firefox)

could someone please tell me how i can stop this annoying behaviour? I have tried some scripts to disable the key, however the user still needs to be able to add these double lines and most scripts disable the action, eg

Code:
function handleEnter(field, event) {
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if (keyCode == 13) {
			return false;
		} 
		else
		  return true;
	}

I notice the textarea i am currently typing into here at tek-tips does not have this issue..

Cheers [cheers] in advance



Tracey
Remember... True happiness is not getting what you want...

Its wanting what you have got!
 
Ok cancel this question, it was caused by a function called onkeypress of the form which was enabling form post on enter within an input.

I adjusted the code, fyi here:

Code:
function enterkey(event){
if (document.layers)
  document.captureEvents(Event.KEYDOWN);
  document.onkeydown =
    function (evt) { 
      var keyCode = evt ? (evt.which ? evt.which : evt.keyCode) : event.keyCode;

//ADDED THIS BLOCK OF CODE	
        //set myEvent to browser compatible property
	if (!event){ var myEvent = window.event;}
	else{var myEvent = event;}
	//set targ to browser compatible target
	if (myEvent.target) targ = myEvent.target;
	else if (myEvent.srcElement) targ = myEvent.srcElement;

      if (keyCode == 13)   //13 = the code for pressing ENTER 
      {
        //ADDED CONDITION
        if (!targ.type == "textarea")
         document.forms[0].submit();
      }
    }
}

Tracey
Remember... True happiness is not getting what you want...

Its wanting what you have got!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top