-1

I want select auto radio button with Greasemonkey. But I want select radio with label name. When I see that four radio button, Greasemonkey need select my option. Example: I want auto select label Instagram.

<label for="ContentPlaceHolder1_answer3">Instagram</label>

How can I select this radio button with Greasemonkey?

<div class="row">
              <input type="radio" tabindex="3" value="answer1" name="ctl00$ContentPlaceHolder1$1" id="ContentPlaceHolder1_answer1"><label for="ContentPlaceHolder1_answer1">Twitpic</label></div>

<div class="row">
            <input type="radio" tabindex="4" value="answer2" name="ctl00$ContentPlaceHolder1$1" id="ContentPlaceHolder1_answer2"><label for="ContentPlaceHolder1_answer2">Yfrog</label></div>

<div class="row">
            <input type="radio" tabindex="5" value="answer3" name="ctl00$ContentPlaceHolder1$1" id="ContentPlaceHolder1_answer3"><label for="ContentPlaceHolder1_answer3">Instagram</label></div>

<div class="row">
            <input type="radio" tabindex="6" value="answer4" name="ctl00$ContentPlaceHolder1$1" id="ContentPlaceHolder1_answer4"><label for="ContentPlaceHolder1_answer4">Flickr</label></div>

 </div>


hcttepe
  • 157
  • 1
  • 6
  • 11

3 Answers3

1

Try this:

var label="whatever"; //The label you want to look for, you may already have some such variable
var allLabels=document.getElementsByTagName("label"); //Get all labels
for (var i=0;i<allLabels.length;i++) { //Loop through labels
    if (allLabels[i].innerHTML.toLowerCase()==label) { //If a match is found...
        allLabels[i].parentNode.getElementsByTagName("input")[0].checked=true; //Check the parent element for inputs (radio buttons, for instance), and check the first one
    }
}

I can't tell for sure if that will work without knowing how the HTML is put together. You gave the HTML in your question, but I don't know if it retains that constant format.

Ashley Strout
  • 6,107
  • 5
  • 25
  • 45
  • Thanks your answer but it's have a lot of questions so i need select them with label name. How can i select them with label name ? – hcttepe Feb 07 '12 at 00:54
  • I have updated the answer. Be sure to read the last bit. – Ashley Strout Feb 07 '12 at 01:02
  • hmm. I don't have coding information. Now ı didn't understand them. I want explain details. I have 95 questions and i know all true answer but i want select them auto. – hcttepe Feb 07 '12 at 01:14
1

I've not used xpath in a while, so it might not do anything :)

But in theory, this will search for all labels where the text constains "Instagram", and then it loops through all the results, taking the for attribute to get the radio button and sets it's checked to true. (although i think its "selected" for radio buttons?)

function xpathSnapshot(expression,context) {
    var r=document.evaluate(expression,context,null,6,null),i=r.snapshotLength-1;
    this.iterate=function() {return r.snapshotItem(i--)}
}

var labels = xpathSnapshot("//label[contains(text(),\"Instagram\")]",document);

while (label = labels.iterate()) {
    document.getElementById(label.getAttribute("for")).checked = true;
}
NickSlash
  • 4,758
  • 3
  • 21
  • 38
0
var labels = document.getElementsByTagName('label'); //get the labels
for (var i = 0; i < labels.length; ++i) { //loop through the labels
    if (labels[i].textContent == "Instagram") { //check label text
        labels[i].click(); //if correct text, click the label
    }
}
Anon
  • 16