1

I am trying to manually submit a form in Chrome's developer console by running code like:

$('form').submit();

How can I specify the input value of the form in this code? I know the input id/name.

grg
  • 5,023
  • 3
  • 34
  • 50
Taewan
  • 1,167
  • 4
  • 15
  • 25
  • that method will submit all the values of your form elements with `name` tags inside that `form` block. – bman Sep 02 '16 at 03:29

1 Answers1

9
document.querySelector('input[name="theNameOfYourInput"]').value = 'theValue';
document.querySelector('form').submit();

Chrome console knows nothing of jQuery. Use vanilla JS.

  • 1
    It does know about `jQ` id it is included in `webpage` – Rayon Sep 02 '16 at 03:40
  • I'm pretty sure Chrome was the first browser to introduce a "security" feature where you can't access page variables/functions from within the console or a userscript. There is a [way](http://stackoverflow.com/questions/7474354/include-jquery-in-the-javascript-console) to run jQuery from within the console, but I don't see a reason for it in this case. –  Sep 02 '16 at 03:51
  • You can access any globally scoped objects/functions from the page in the Console. You can access locally scoped ones too when you are paused with the debugger. – Gideon Pyzer Sep 02 '16 at 09:10
  • @Gideon, by "global" you mean assigned to the window object or referenced in a global context? What do you mean by "paused with the debugger"? If that's true, then their philosophy on userscripts is even more convoluted. I see no reason to prohibit within userscripts what is allowed from within the console, it makes no sense. –  Sep 02 '16 at 09:16
  • 1
    @VictorP. In the browser, anything you define globally (i.e. outside of functions) is added to the `window` object. In the Console, you can access anything on that object. If you use the debugger and pause execution within a function, you will have a call stack and local variable panel. You can navigate to the Console and access anything in the current scope. As for user scripts, I'm able to access objects assigned to `window` fine. – Gideon Pyzer Sep 02 '16 at 09:47
  • @Gideon, thank you! I'll have to refurbish my knowledge. –  Sep 03 '16 at 02:35