I am writing a Greasemonkey/Tampermonkey script and I need to turn on radio buttons depending on the name and value of the radio control.
This is how it looks:
<input type="radio" name="11" value="XXX" class="radio">
<input type="radio" name="11" value="zzzz" class="radio">
So I want to set those buttons that have name of 11 and a value of zzzz to be checked.
This is super easy when you use jQuery. Here’s a complete script:
The
input[name='11'][value='zzzz']jQuery selector gets all<input>s that have the initial valuezzzz, but only if they are in the group of radio buttons withname="11".Reference:
.prop()Important: The above works on most pages but, on AJAX-driven pages, you often need to use AJAX-aware techniques. This is because the Greasemonkey script will run before the checkbox you care about is loaded.
One way to deal with this is with the
waitForKeyElementsutility. Like so:Old answer that was “state of the art” 🙂 back when the question was asked: