3

I have a simple form that looks like this:

<form action="#" name="submit" method="post"> <input name="zip" id="zip" type="text" value="" class="zip-start" /> <div class="form-submit"><a href="" id="zipformSubmit" rel="zipformSubmit" class="zipformSubmit">Submit</a></div> </form> 

I have JS that looks like this:

<script> $(document).ready(function() { $("a[rel=zipformSubmit]").click(function(e) { e.preventDefault(); var zip = $('#zip').val(); if (zip.length < 5) { alert("Please enter a valid zip code"); return false;}; $.colorbox({href: "/ppc/form.htm?zip=" + zip, iframe:true, innerWidth:800, innerHeight:600}); }); }); </script> 

The problem is that the user can only submit this form by clicking on the submit button. Any ideas on how i can adjust the js so that the enter button also submits the form?

thanks in advance

3
  • What is the 'enter button'? It's not clear above. Commented Aug 26, 2011 at 23:45
  • @JJ, enter key on your keyboard! :) Commented Aug 26, 2011 at 23:48
  • @rob melino, did I answer your question completely? Commented Aug 26, 2011 at 23:52

2 Answers 2

1

Use as well jQuery's submit [docs] method to listen for an form submit from clicking enter.

<script> function submitHandler(e) { e.preventDefault(); var zip = $('#zip').val(); if (zip.length < 5) { alert("Please enter a valid zip code"); return false;}; $.colorbox({href: "/ppc/form.htm?zip=" + zip, iframe:true, innerWidth:800, innerHeight:600}); } $(document).ready(function() { $('form').submit(function(e) { submitHandler(e); }); $("a[rel=zipformSubmit]").click(function(e) { submitHandler(e); }); }); </script> 

You should however change this $('form') to something more specific. Add an id for the form and it should be perfect.

Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried using $(...).submit()?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.