How can I click an item by condition with Greasemonkey?

<input type="hidden" name="my_id" id="my_id" value="1">

<ul class="myList clearfix" id="thismyList">
<li class="myBullet" id="answer1">blabla1</li>
<li class="myBullet" id="answer2">blabla2</li>
<li class="myBullet" id="answer3">blabla3</li>

In this page I want to something like this:

if document.getElementById('my_id').value=="1"
document.getElementById('answer1').click;
if document.getElementById('my_id').value=="2"
document.getElementById('answer3').click;
if document.getElementById('my_id').value=="3"
document.getElementById('answer1').click;
if document.getElementById('my_id').value=="4"
document.getElementById('answer2').click;

I write this to show what I want. I know that code is wrong. But I think that explains what I want. How can I do that?

Answers


The correct syntax for that would be:

switch (document.getElementById('my_id').value) {
    case "1":
        document.getElementById ('answer1').click ();
        break;
    case "2":
        document.getElementById ('answer3').click ();
        break;
    case "3":
        document.getElementById ('answer1').click ();
        break;
    case "4":
        document.getElementById ('answer2').click ();
        break;
    default:
        break;
}

Note that in some cases, you may need to trigger mouse events like in this answer.


IF you want to invoke the click behaviour of a function you should just call.

document.getElementById('id').click();

I think it should works like you did without the '()' also.


Need Your Help

Github API v3 POST with PHP

php github github-api

I'm attempting to use the Github v3 API and posting JSON to update a profile (or some other call) and get the following response from Github;

Right to Left (Hebrew) support in IntelliJ Community Edition

intellij-idea right-to-left

Is there a plugin or an easy way to write right to left in IntelliJ Community Edition?