Hi everybody,
I have a question:
:-/
I want to submit a form in an HTML page by pressing Enter and it does not submit if I press Ctrl+Enter

Recommended Answers

All 6 Replies

Maybe you did not understand my question: :)
my question was:
I want to submit my form by pressing "Enter" via the keyboard...
onpress enter="form.submit"
Thank you.

A form is by default submitted when you press the return key. You need to show us how your form is structured so that we can offer more suggestions. Paste your code here with code tags.

Sorry, my question wasn't clear enough... :)
I want the form to submit when I press "enter" even if I was focusing on a Textarea..
Did you understand?
Thank you.

Try this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled</title>
<script type="text/javascript"><!--
function sendIt(formElem)
{
 if (event.keyCode==13 )
{
   formElem.submit();
   return true;
}
 return false;
}
//--></script>
</head>
<body>
<form action="http://www.somesite.com" onkeypress="sendIt(this)">
<textarea rows="10" cols="70"></textarea>
</form>
</body>
</html>

You need to attach an event listener to the onkeypress event handler of the form element which would monitor the key strokes and submit the form as soon as a RETURN key is pressed.

<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>	
	<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
	<title>OK</title>
	<script type="text/javascript">

	function checkSubmit(e) {
		e = e || window.event;
		var code = e.keyCode || e.which;
		if(code == 13 /* return key pressed */) {
			var src = e.srcElement || e.target;
			if(!!src && !!src.form)
				src.form.submit();
		}			
	}
	</script>
</head>
<body id="bodyMain">
	<form action="/action.do" method="get" onkeypress="checkSubmit(event);">
		<select name="sel" multiple="multiple">
			<option value="one">1</option>
			<option value="two">2</option>
			<option value="three">3</option>
		</select>
		<textarea name="txtArea"></textarea>
		<input type="text" id="txtId" value="OK" />
		<input type="text" name="txtName" value="txtName" />
		<input type="button" value="Submit" />
	</form>
</body>
</html>
function checkSubmit(e) {
        e = e || window.event;
        var code = e.keyCode || e.which;
        if(code == 13 /* return key pressed */) {
            var src = e.srcElement || e.target;
            if(!!src && !!src.form)
                src.form.submit();
        }           
    }
    </script>

May I ask you to explain please?
:-/

what is ||
and what is .which
and what are .srcElement and .target?
please explain 'cause I did not understand that. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.