Skip to content Skip to sidebar Skip to footer

How Can I Auto-select A Radio Button With Greasemonkey?

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 a

Solution 1:

Try this:

var label="whatever"; //The label you want to look for, you may already have some such variablevar allLabels=document.getElementsByTagName("label"); //Get all labelsfor (var i=0;i<allLabels.length;i++) { //Loop through labelsif (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.

Solution 2:

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?)

functionxpathSnapshot(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;
}

Solution 3:

var labels = document.getElementsByTagName('label'); //get the labelsfor (var i = 0; i < labels.length; ++i) { //loop through the labelsif (labels[i].textContent == "Instagram") { //check label text
        labels[i].click(); //if correct text, click the label
    }
}

Post a Comment for "How Can I Auto-select A Radio Button With Greasemonkey?"