0
<a href="javascript:void(0)" class="PrmryBtnMed"
 id = "VERYLONGTEXT"

onclick="$(this).parents('form').submit(); return false;"><span>Dispatch to this address</span></a>

If I was to give instructions to a human, I would say:

  1. look for a <span>Dispatch to this address</span> link; if it appears click it. (if there is more than one such link (for example there are two) just click the first one (or any of them)

I am using Greasekit, so I am looking to do this using JavaScript.

Thank you!

Rune FS
  • 21,497
  • 7
  • 62
  • 96
Elle
  • 375
  • 1
  • 2
  • 12

1 Answers1

2

Updated: Now checks contents of element

var el = document.querySelector(".PrmryBtnMed"); //should only return first one
if (el && (el.textContent.trim() == 'Dispatch to this address')) {
  el.click();
} 

Have a look at the querySelector and textContent for more information

I also added a JsFiddle for you: http://jsfiddle.net/NrGVq/1/


A different approach is to search the whole page, in case it's not inside the matched element

var inPage = document.documentElement.innerHTML.indexOf('text to search') > 0,
    el = document.querySelector(".PrmryBtnMed");

if (inPage && el) el.click();
Dogoku
  • 4,585
  • 3
  • 24
  • 34
  • Thanks very much, but I'm using **Greasekit** so I'm not sure i can use JQuery. (Can it be done with javascript?) (or, does anyone know **how to load JQuery into greasekit**? I'm going to try and google the answer) – Elle Apr 24 '13 at 14:42
  • this is not jQuery, this is native javascript. Works in modern browsers and IE8+ – Dogoku Apr 24 '13 at 14:51
  • If you still want to add jQuery to greasekit, for other uses, have a look at http://thepugautomatic.com/2009/07/jquery-in-greasekit/ – Dogoku Apr 24 '13 at 15:00
  • Ah, it's native JavaScript, **I stand corrected**. However, upon reflection, I can't use this method as will get undesired clicks on similar pages (that also use `PrmryBtnMed`). Is it possible for the code to only run if `Select a shipping address` appears on the page? Alternately, if it's easier to search for a single word, what if the word `Eskdale` appears... only then click the button. (Thanks!) – Elle Apr 25 '13 at 10:32
  • Updated my answer to search for text both inside the element or anywhere in the page – Dogoku Apr 25 '13 at 12:24
  • I need to tweak this: the class name has changed from `PrmryBtnMed` to `PrmryBtnMed ApricotWheat`. As a result, what changes do I need to make to `el = document.querySelector(".PrmryBtnMed”);` to make it work with the apricot thing, please? – Elle Apr 26 '15 at 03:02
  • `document.querySelector(".PrmryBtnMed")` will pickup any elements that have `PrmryBtnMed` as a class. If you want *only* the elements that have both classes use `document.querySelector(".PrmryBtnMed.ApricotWheat")` – Dogoku Apr 27 '15 at 15:37