views:

133

answers:

4

I have 25 components which includes [textarea, textfile, radio, combo, etc...] and I have written a key event so that when "ENTER" is entered, I call a function which will submit the page.

Now my page is getting submitted when I press enter, even in the textarea which should not be. So is there any way that I can not submit the page if it is pressed in the text area?

This happens only in IE7 and IE8; it works properly in all the other browser.

A: 

you could probably detect if any of the textarea, etc is not filled out/emtpy/unset. if all of them are filled out properly, send the form.

contagious
A: 

Did you attach the "key event" to the whole form? The whole DOM? if you did that's a normal behavior.

If you want the "Enter key" to submit the page when the focus is on the submit button then apply this functionality in the onsubmit event - there of course you can perform all the validation you need. If you just want to exclude the enter key event from the text area - perform a simple check if the the focus is in the textarea that momemnt.

StarScream
A: 

The default behaviour of a form is to submit if the user hits enter inside the form unless the focus is on a textarea, so what you want is the default behaviour. Remove whatever code you have that currently handles keypresses for the form and you'll have what you want.

Tim Down
i have a function where i forcefully submit the page on enter. and its not anymore default. so now this is the codeif (browserName == "Microsoft Internet Explorer" event.cancel = true; elements[item].click(); return; }so now, before clicking, if i can check if the cursor is in textarea i don't else will clickhow do i get the component where the cursor is?
pradheep
Sorry, I don't think I understand. Why do you have code to handle enter? Does the form not do what you want when you remove the code? If not, how is it bahving differently to what you want?
Tim Down
A: 

I'm not sure if this will suit your needs, but you can disable the enter key inside the textarea with something like this:

$(document).ready(function(){
 $('textarea').keypress(function(e){
  var key = (window.event) ? e.keyCode : e.which;
  if ( key == 13 ) {
   return false;
  } else {
   return true;
  }
 })
})
fudgey