?
Solved

jquery keypress function is also activating form submission

Posted on 2014-03-05
2
Medium Priority
?
356 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 9

Accepted Solution

by:
WebDevEM earned 2000 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

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

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

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

765 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