3

enter image description here

These two buttons are from gmail. They let you go left and right through more emails.

How do I click these buttons using javascript and jquery? I'm willing to use extra libraries if necessary. I'm trying to make a shortcut that lets me use keyboard buttons to click and go to older or newer emails.

Charles Haro
  • 1,866
  • 3
  • 22
  • 36

3 Answers3

2

let me know if it helps:-

$(document).keydown(function(e) {
    switch(e.which) {
        case 37: // left
        break;    

        case 39: // right
        break;    

        default: return; // exit this handler for other keys
    }
    e.preventDefault(); // prevent the default action (scroll / move caret)
});
Sharique Ansari
  • 534
  • 5
  • 12
  • No this doesn't, I want to click the button using javascript, So far trying jquery .click and simulating a mouse click do not work. – Charles Haro Mar 13 '15 at 08:08
  • @CharlesHaro you said you are trying to make a shortcut. if you want to click that button using javascript you can do like this `var myEl = document.getElementById('myelement'); myEl.addEventListener('click', function() { alert('Hello world'); }, false);` – Sharique Ansari Mar 13 '15 at 09:25
  • @CharlesHaro if click is defined you can simply use this `document.getElementById("myelement").click();` – Sharique Ansari Mar 13 '15 at 09:28
  • This does not work on the gmail buttons. There is something else going on. Go try it for youself – Charles Haro Mar 15 '15 at 00:50
  • @CharlesHaro can you add your html in `jsfiddle` – Sharique Ansari Mar 16 '15 at 08:36
2

you aren't going to be able to just sit a script on gmail. You will either need to create this in google scripts or will need to use something like Gmailr in order to inject the script into the DOM

RadleyMith
  • 1,313
  • 11
  • 22
  • are you sure? All the other buttons I've tried have worked fine, like the compose button I can make a shortcut for that – Charles Haro Mar 18 '15 at 19:01
  • Bradgnar, that's not true. bookmarklets can be inject javascript into any page including Gmail. – omdel Mar 19 '15 at 19:51
  • its not inserting the js that is the problem, its just that every thing i've tried does not work for clicking that button. – Charles Haro Mar 19 '15 at 23:39
2

Try this out from the JS console. No jquery required, so you wont have to manually inject the jQuery library to get it to work:

document.querySelectorAll("[data-tooltip='Newer']")[0].click()

... Thing is, the whole DOM is minimized, so I cant guarantee that any IDs or class-names will remain the same from one day to the next... I bet the tooltip will stay the same for longer, though :)

MattE_WI
  • 377
  • 1
  • 11