1

I have a website that I cannot edit that I visit frequently. I'm using Both Greasemonkey for Firefox and Tampermonkey for Chrome to try to use Javascript to automatically select a value in a dropdown. Below is the element of the dropdown.

<select name="term">
    <option value="12">12 /mo. - $20.95/mo.</option>
    <option value="24">24 /mo. - $17.95/mo.</option>
    <option value="36">36 /mo. - $15.95/mo.</option>
</select>

As you can see, there is no element id. I've tried following the answers on here and here but changed them to use document.getElementsByName instead of document.getElementById since this dropdown element doesn't have an id but nothing I've done has worked.

How can I use Greasemonkey or Tampermonkey to automatically select the value of "36" whenever I visit that webpage?

Community
  • 1
  • 1

3 Answers3

3

You can access an element by name with an attribute selector. When you set its value, that selects the option with that value.

$("select[name=term]").val("36");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="term">
    <option value="12">12 /mo. - $20.95/mo.</option>
    <option value="24">24 /mo. - $17.95/mo.</option>
    <option value="36">36 /mo. - $15.95/mo.</option>
</select>
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I tried this and it still didn't do anything. Not sure what I'm doing wrong. Does it make a difference if there is more than one dropdown on this webpage with basically the same elements and values? – Mister Pyrrhuloxia Oct 06 '15 at 20:57
  • This will update all of the dropdowns with `name="term"` – Barmar Oct 06 '15 at 21:03
  • Added an execuable snippet to show that it works. I don't know if doing it from GreaseMonkey makes a difference, though. – Barmar Oct 06 '15 at 21:05
  • So I did what the person below suggested (http://stackoverflow.com/a/32979483/5309708): `// @require http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js` `// ==/UserScript==` `$("select[name=term]").val("36");` But it didn't work. – Mister Pyrrhuloxia Oct 06 '15 at 21:05
  • No, sorry. I don't know what changes when running code in GreaseMonkey or TamperMonkey. – Barmar Oct 06 '15 at 21:06
2

// ==UserScript==
// @name         http://stackoverflow.com/questions/32979378
// @match        http://website.with.dropdown.com/*
// @grant        none
// ==/UserScript==
(document.querySelector && document.querySelector('select[name="term"]') || []).value = '36';
<select name="term">
  <option value="12">12 /mo. - $20.95/mo.</option>
  <option value="24">24 /mo. - $17.95/mo.</option>
  <option value="36">36 /mo. - $15.95/mo.</option>
</select>
zanetu
  • 3,740
  • 1
  • 21
  • 17
0

Looks like you can use jQuery in greasemonkey with

// @require       http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js

How can I use jQuery in Greasemonkey?

then the jQuery selector would be along the lines of:

$("select[name=term]")

https://api.jquery.com/attribute-equals-selector/

Community
  • 1
  • 1
pherris
  • 17,195
  • 8
  • 42
  • 58
  • I tried this and it still didn't do anything. Not sure what I'm doing wrong. Does it make a difference if there is more than one dropdown on this webpage with basically the same elements and values? – Mister Pyrrhuloxia Oct 06 '15 at 20:57