know which button was clicked via JavaScript

0

Hello! someone could help me in this, I'm trying to make an application to do certain calculations but I'm having some difficulty when doing Onclick. so below is my function Initializes what I need is that when the user clicks the button they send to the eventClick function and in it can identify the button ID

function inicializa() {
    document.write("<head>");
    document.write("<title>Pagina Principal</title>");
    document.write("<meta name='viewport' content='width=device-width, initial-scale=1.0'>");
    document.write("<link type='text/css' href='css/bootstrap.min.css' rel='stylesheet'>");
    document.write("<script type='text/javascript' src='js/bootstrap.min.js'></script>");
    document.write("<script type='text/javascript' src='js/appControler.js'></script>");
    document.write("</head>");
    document.write("<body>");
    document.write("<nav class='navbar navbar-fixed-top navbar-inverse'>");
    document.write("<div class='navbar-header'>");
    document.write("<div class='navbar-header'>");
    document.write("<a onload='inicializa()' class='navbar-brand'>Calculadora</a>");
    document.write("</div>");
    document.write("</div>");
    document.write("<center>");
    document.write("<img src='imagens/calculadora.png' alt='Calculadora' class='img-thumbnail'>");
    document.write(" <div class='btn-group' id='DivButton'>");
    document.write("  <button onclick='eventoClick()' id='Delta' class='btn btn-primary'>Delta</button>");
    document.write("  <button onclick='eventoClick()' id='Pitagoras' class='btn btn-primary'>Pitagoras</button>");
    document.write("  <button onclick='eventoClick()' id='JurosSimples' class='btn btn-primary'>Juros Simples</button>");
    document.write("  </div>");         
    document.write("</center>");
    document.write("</body>");
}

Could you help me?

    
asked by anonymous 15.06.2018 / 02:02

1 answer

0

Put a this as a function parameter in onclick :

onclick='eventoClick(this)'

And in the function you get the id of the clicked element:

function eventoClick(e){
   var but_id = e.id;
}

Example:

function inicializa() {
    document.write("<body>");
    document.write("<nav class='navbar navbar-fixed-top navbar-inverse'>");
    document.write("<div class='navbar-header'>");
    document.write("<div class='navbar-header'>");
    document.write("<a onload='inicializa()' class='navbar-brand'>Calculadora</a>");
    document.write("</div>");
    document.write("</div>");
    document.write("<center>");
    document.write("<img src='imagens/calculadora.png' alt='Calculadora' class='img-thumbnail'>");
    document.write(" <div class='btn-group' id='DivButton'>");
    document.write("  <button onclick='eventoClick(this)' id='Delta' class='btn btn-primary'>Delta</button>");
    document.write("  <button onclick='eventoClick(this)' id='Pitagoras' class='btn btn-primary'>Pitagoras</button>");
    document.write("  <button onclick='eventoClick(this)' id='JurosSimples' class='btn btn-primary'>Juros Simples</button>");
    document.write("  </div>");         
    document.write("</center>");
    document.write("</body>");
}
inicializa();

function eventoClick(e){
   console.log(e.id);
}
    
15.06.2018 / 02:11