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

0

I need to execute the action of this button, which originally works with Control + Enter, using my JS, but the only static things on the button are 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 key simulation, which acts directly on the element and allows me to command the sending programmatically.

    
asked by anonymous 02.10.2014 / 13:12

2 answers

0

You can use document.querySelectorAll () . This method works in a similar way to jQuery, searching for CSS selectors. Just look at browsers support for this function to see if its reach meets your needs.

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

First step find the button. We know it's a div and 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 asked to load two keys then, we almost certainly have a keypress event, which I try to cancel. Ideally, you should know what function the event calls and uses the removeEventListener ();

In any case, you can use the first button to perform the action you want. If you can put a little more code.

    
02.10.2014 / 19:43