Solved

jquery keypress function is also activating form submission

Posted on 2014-03-05
2
345 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

839 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