24

I'm trying to change a checkbox value from chrome JavaScript console. I'm aware that changes are printed to object and not on screen. I'd like to execute a jQuery statement, i.e.:

('input[name=foo]').attr('checked', true);

Is there any way to enter a jQuery statement, and and see changes instantly in the page?

p.campbell
  • 98,673
  • 67
  • 256
  • 322
damir
  • 1,898
  • 3
  • 16
  • 23

4 Answers4

39

Yeah, and even if you don't have jquery available let it be :)

Edited snippet from How can I use jQuery in Greasemonkey scripts in Google Chrome?

var script = document.createElement("script");
  script.setAttribute("src", "https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js");
  script.addEventListener('load', function() {
    var script = document.createElement("script");
    document.body.appendChild(script);
  }, false);
  document.body.appendChild(script);

After this go.

  • 1
    See also using bookmarklet. It is even nicer and compact. http://stackoverflow.com/a/9624997/805366 – Santeri Vesalainen Aug 04 '12 at 20:36
  • Failed here on latest Chrome: "Mixed Content: The page at 'https://[snip]' was loaded over HTTPS, but requested an insecure script" Fix: on line 2, change http to https – ChrisJJ Aug 31 '16 at 20:45
  • could I guess do in the address bar all of that within . `javscript:!function(){........}()` . i.e. `javascript:function(){var script = document.createElement("script"); script.setAttribute("src", "https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"); script.addEventListener('load', function() { var script = document.createElement("script"); document.body.appendChild(script); }, false); document.body.appendChild(script);}();` . Then you can make a bookmark for it – barlop Apr 07 '19 at 23:15
6

In chrome console you can do run any JavaScript, so if page has jQuery loaded, you can access the element and check/uncheck it e.g. stackoverflow.com (this site) uses jQuery so I can just access answer textarea and enter a value, try this

$('#wmd-input').val('This is my answer') 

So you can do anything which is possible in jQuery.

Anurag Uniyal
  • 85,954
  • 40
  • 175
  • 219
  • there is a default loaded jquery but it is limited e.g. try `$.ajax` it doesn't work by default, even though $ shows as a function and even though you can do $('html') – barlop Apr 07 '19 at 23:16
  • https://stackoverflow.com/questions/45757627/what-is-in-chrome-console#:~:text=%24%20is%20an%20alias%20for%20document,Command%20line%20(console)%20api. – Yashdeep Hinge May 04 '23 at 06:04
1

If you got jQuery loaded in the current page, you can type jQuery command.

Just type $('input[name=foo]').attr('checked', true); or whatever should work in jQuery and press enter.

Shikiryu
  • 10,180
  • 8
  • 49
  • 75
0
$('input[name=foo]').click();

does the same thing as clicking with your mouse.

sje397
  • 41,293
  • 8
  • 87
  • 103