1

I'm trying to submit a field with jquery, normally in the page you would click on a submit button or press enter in the field. I'm doing this in a chrome script. Now if I type:

$('#myButton').click()

In the Console, this actually works fine, but when i call the same code from my script function, it doesn't work. I tried also to use this:

var e = jQuery.Event("keydown");
e.which = 50; 
e.keyCode = 50;
$("#myButotn").trigger(e);

but still no results.

To be specific, the button that i'm willing to click has an onClick="submitFunction()"

thestral
  • 971
  • 1
  • 6
  • 12

3 Answers3

2

Here is a much better way to trigger a click on keypress.

$(window).keydown(function (e) {
    if ( e.which === 50 ) {   
         $("#myButton").click();     
    }
});

Demo: http://jsfiddle.net/bc7r3vq1/1/

By the way, keycode 50 is the number 2

Chan
  • 269
  • 1
  • 13
  • I can't call the function, or at least i don't know how, since the function is into the js page, my script in a chrome extension – thestral Apr 06 '15 at 15:38
  • No problem! Please try the updated code and demo – Chan Apr 06 '15 at 15:49
  • I tried it, it doesn't work.. But anyway I think your function presses the button when I press Enter, (or 2 in this case) – thestral Apr 06 '15 at 16:03
0

Now, i've found a solution, but i'm not sure which one was the problem. First of all I started using tampermonkeys, from there i used

$('#textField').submit();

and it worked fine.

I'm going to check now if I had to use this method, or if it was just something related with my extension.

EDIT: It works only if I use tampermonkeys, as they saied..

thestral
  • 971
  • 1
  • 6
  • 12
  • Tampermonkey essentially executes the script in the page's context, that's why it works. – Xan Apr 06 '15 at 22:33
  • @Xan Yes you are right, I tried it aswell. Now, despite I fixed it using Tampermonkey, if in the future I will do an extension that i'm willing to put in the store, how can I fix this problem? – thestral Apr 07 '15 at 09:21
  • Is the page you work with guaranteed to have its own jQuery? You work only with specific pages, right? – Xan Apr 07 '15 at 09:27
  • Mm I've not idea of what I'll do in the future, for now I'm just doing small scripts for my own.. Anyway, I had to include jquery in my script of tampermonkeys otherwise it would throw "$ is not defined" so I'm supposing it was using my jq – thestral Apr 07 '15 at 09:35
  • A generic answer is hard. You would need to simulate the event on the DOM level instead of using jQuery events. A concrete answer (some specific page) may have a simpler answer. – Xan Apr 07 '15 at 09:38
  • Ok thanks, but why had I to import jquery in my tampermonkey script if it uses the page's one? – thestral Apr 07 '15 at 10:12
  • That I'm not sure. I've never had to deal with Tampermonkey engine in detail. – Xan Apr 07 '15 at 10:16
-1

I believe your issue comes from the browser policy preventing your code from faking a user action. Chrome only allows you to fake a user action if its called directly from another user action handler like an actual user click of keydown. Try your code from within a click handler and if it works its because of this policy block.

Zig Mandel
  • 19,571
  • 5
  • 26
  • 36
  • Not the problem here. It's just a question of jQuery copy in a content script not triggering events in page's copy of jQuery. That's why it works from the console. – Xan Apr 06 '15 at 17:30
  • Hmm ive done this before from a chrome extension content script and faked events did get triggered in the page and handled by the original page code. But only from a user action. – Zig Mandel Apr 06 '15 at 18:32