2

I've got a form, where i put information in with greasemonkey. The form got no ID or Class...

When you manauly put something in the inputbox, and press enter, it get's submitted and you're off... I want to achief the same with jquery.

Changing the value's of the input boc is not the problem, but submitting.

Ive got the following code;

e = jQuery.Event("keypress");
    e.which = 13;
    e.keyCode = 13;
    $("#inputy").focus();
    $("#inputy").trigger(e);

But this doesn't work... So how does i simulate an enter in the inputbox with the ID inputy?

Greetz TWCrap

rodrigoap
  • 7,405
  • 35
  • 46
Mathlight
  • 6,436
  • 17
  • 62
  • 107

2 Answers2

2

So you want to submit the form? You can try:

$("#inputy").keyup( function( event )
{
    if(event.keyCode == 13)
    {
        var form = $( "#inputy" ).closest( "form" );
        form.submit( );
    }
});

On every key-up we check for the Enter key. If the Enter key was released we then find the closest form and submit it.

ssell
  • 6,429
  • 2
  • 34
  • 49
  • problem... I'm adding the data to the form dynamicly with jqeury... So when i add the data, i want to submit. Not adding the data with jqeury, and pressing enter by myself... – Mathlight Jan 31 '12 at 14:46
  • Ah. Then whenever you are done adding the data, can you not call `form.submit( )`? If you don't know the form itself, just use `.closest( "form" )` on whatever the last field you added data to. – ssell Jan 31 '12 at 14:48
0

Probably pressing enter on <input> fire submit() method on entire <form> so try do it after you fill up your <input> using following method: $("form").submit().

Anyway, is there jquery for sure on that site? Are there any errors on console?

IProblemFactory
  • 9,551
  • 8
  • 50
  • 66
  • As menthiod above, i don't want to press the enter button by myself, i want to do it with jquery. And yes, Jquery is permitted and no errors. – Mathlight Jan 31 '12 at 14:49
  • I mean use jquery method `$("form").submit()` , word `manually` was my mistake :) – IProblemFactory Jan 31 '12 at 15:02
  • haha, could always happen ;P ssell have answerd my question already, but thanks for your opinion ;P. And that would probally not work, because there more forms on the site, so i wouldn't work unless i added closest (damm wat am i learning today :P) – Mathlight Jan 31 '12 at 15:06