Solved

jquery keypress function is also activating form submission

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

691 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