Solved

jquery keypress function is also activating form submission

Posted on 2014-03-05
2
343 Views
Last Modified: 2014-03-05
I have the following jquery keypress function:
$(document).ready(function(){
	$("#tocall").keypress(function(e) {  
		if (e.which == 13) {
			params = { "tocall" : $('#tocall').val()};
			connection = Twilio.Device.connect(params);
	}
	...

Open in new window

This code also activates the following code submission tag:
<INPUT type='submit' value='DELETE'>

Open in new window

Why is it doing this and how do I stop it?

Attached is the entire php script in case you need to see it.
recordings-no-sid.php
0
Comment
Question by:thenelson
2 Comments
 
LVL 9

Accepted Solution

by:
WebDevEM earned 500 total points
ID: 39907655
Hi,

I'm running out the door so I don't have time to test, but try adding e.preventDefault(); like so:
$(document).ready(function(){
	$("#tocall").keypress(function(e) {  
		if (e.which == 13) {
                        e.preventDefault();
			params = { "tocall" : $('#tocall').val()};
			connection = Twilio.Device.connect(params);
	}
	...

Open in new window

The jQuery isn't actually triggering the form submission, that's caused by the ENTER key itself.  What the preventDefault() does is tell the browser to ignore the normal action caused by pressing ENTER.

Hope this helps... I'll be back online in the morning.

WebDevEM

Reference: http://api.jquery.com/event.preventdefault/
0
 
LVL 39

Author Closing Comment

by:thenelson
ID: 39907818
That took care of it! And thanks for the explanation.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question