I need help…
I want to click a radio button automatically…but also need to check the sentence (label)…
eg…() <-radio button
<label>How much is 1+1?</label><br/>
<input type="radio" name="group1" value="2"/>()2<br/>
<input type="radio" name="group1" value="3"/>()3<br/>
<input type="radio" name="group1" value="4"/>()4
I tried use this script:
// ==UserScript==
// @name script
// @description Auto select rpt radio button
// @include *
// ==/UserScript==
if(radio=document.evaluate('//input[@type="radio" and @value="2"]',document,null,9,null).singleNodeValue)
radio.checked=true;
Ok, this code..click answer ()2
but what happen if i have the following:
<label>How much is 1+1?</label><br/>
<input type="radio" name="group1" value="2"/>()2<br/>
<input type="radio" name="group1" value="3"/>()3<br/>
<input type="radio" name="group1" value="4"/>()4
How much is 4+2?<br/>
<input type="radio" name="group1" value="8"/>()8<br/>
<input type="radio" name="group1" value="2"/>()2<br/>
<input type="radio" name="group1" value="6"/>()6
Nothing happens because there are two values called “2”..so I tried to modify the code to first check the sentence and then mark the answer (but I have not mastered it very well)…
if(label=document.evaluate('//label[@value="How much is 1+1?"]',document,null,9,null).singleNodeValue)&&(radio=document.evaluate('//input[@type="radio" and @value="2"]',document,null,9,null).singleNodeValue)
radio.checked=true;
My intention was to add a second condition that check the labels with values containing “How much is 1+1?”
Could anyone guide me on how to do that?
Edit:
The sample link:
Google docs form
The page code looks something like this:
Label:
<label class="ss-q-title" for="entry_0">How much is 1+1
<span class="ss-required-asterisk">*</span></label>
Radios:
<ul class="ss-choices"><li class="ss-choice-item"><label class="ss-choice-label"><input name="entry.0.group" value="2" class="ss-q-radio" id="group_0_1" type="radio">
2</label></li> <li class="ss-choice-item"><label class="ss-choice-label"><input name="entry.0.group" value="3" class="ss-q-radio" id="group_0_2" type="radio">
3</label></li> <li class="ss-choice-item"><label class="ss-choice-label"><input name="entry.0.group" value="4" class="ss-q-radio" id="group_0_3" type="radio">
4</label></li>
</ul>
You must find the radio button that is the nearest, following sibling of the
<lable>(or text node) you care about.jQuery makes this kind of thing easier, See the jQuery documentation, especially the Selectors section and the Traversing section.
Here’s a complete Greasemonkey script that will select the right button, based on the new HTML in the question (linked Google doc):
You can also See the code in action at jsFiddle.