How to execute via JS the action of a send email button, whose ID always changes?

Asked

Viewed 653 times

0

I need to perform the action of this button, which originally works with Control+Enter, using a JS of mine, but the only static things of the button are the data-tooltip and aria-label:

<div tabindex="1" class="T-I J-J5-Ji aoO T-I-atl L3 T-I-JW T-I-Zf-aw2" 
id=":go" role="button" aria-label="Enviar ‪(Ctrl-Enter)‬" 
data-tooltip="Enviar ‪(Ctrl-Enter)‬" unselectable="on" 
data-tooltip-delay="800">Enviar</div>

I prefer a solution without simulation of keys, that acts directly in the element and allows me to command the sending programmatically.

  • 1

    See if the question reflects your need, I changed based on the comments you made in my answer. For now I deleted my original answer for not solving your problem, and also help attract new answers.

  • 1

    Now that I realize... Do you want to trigger the Google Mail send button by JS? It sounds interesting, but maybe you have other ways to get the desired result. Put the details in the question, suddenly this is a XY problem and can be solved in other ways.

  • What action do you intend to take? What do you expect the user to do on this button and what do you want to happen after the user action?

2 answers

1

First step find the button. We know that it is a div and that it has two attributes that do not change. Then:

 //buscamos todas as div
   var a = document.getElementsByTagName("div");

   //vemos a que tem o atributo que queremos
   for(var b=0; b<a.length; b++)
   {
     if(a[b].getAttribute("data-tooltip") == "Enviar ‪(Ctrl-Enter)‬"
     {     
        var c= a[b];
        break;
     }
    }

    //cancelamos o evento ligado ás teclas
    try
    {
       c.addEventListener("keypress",function(e){e.preventDefault();return false;})
    } 
    catch(e)
    {
       c.attachEvent("keypress",function(e){e.returnValue = false;return false;})
    }

If you ask to press two keys then we almost certainly have a keypress event, which I try to cancel. The ideal would be to know what function the event calls and use the removeEventListener method();.

Anyway, you can use the first key to perform the action you want. If you can put a little more code.

0


You can use Document.querySelectorAll(). This method works similarly to jQuery, looking for CSS selectors. Just watch out for jQuery browser support for this function to know if the reach of it meets your need.

var button = document.querySelectorAll('[data-tooltip="Enviar ‪(Ctrl-Enter)‬"]')[0]    
button.onclick = function(){ ... }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.