Grab id from clicked button when clicking another button

Asked

Viewed 941 times

0

I need to make sure that when the user clicks on the options button, it keeps this id value and when you click on another "confirm" button it shows the id of the first button clicked.

I tried to do with localstorage, but it ends up showing all the id’s of all buttons.

        <div class="row">
      <div class="col-sm">
        <button type="button" class="btn btn-primary" id="1">EXCEDEU O HORARIO</button>
      </div>
      <div class="col-sm">
        <button type="button" class="btn btn-primary" id="2">CLIENTE REALIZOU</button>
      </div>
      <div class="col-sm">
        <button type="button" class="btn btn-primary" id="3">ERRO DE ENTREGA</button>
      </div>
    </div>

<button type="button" class="btn btn-success but_pag_nao" id="confirm_ob" onClick="ok();">CONFIRMAR</button>
  • Why don’t you use the input type="radio" for that? Another possibility is the use of button group

  • It’s just that I need it the way I reported it :/

2 answers

0


Solution using javascript only

window.idBotaoClicado =0;

function botaoClicado(e){
  window.idBotaoClicado= e
 }

function ok(){
  alert(window.idBotaoClicado)
 }
<div class="row">
      <div class="col-sm">
        <button type="button" class="btn btn-primary" id="1" onclick="botaoClicado(this.id)">EXCEDEU O HORARIO</button>
      </div>
      <div class="col-sm">
        <button type="button" class="btn btn-primary" id="2" onclick="botaoClicado(this.id)">CLIENTE REALIZOU</button>
      </div>
      <div class="col-sm">
        <button type="button" class="btn btn-primary" id="3" onclick="botaoClicado(this.id)">ERRO DE ENTREGA</button>
      </div>
    </div>

<button type="button" class="btn btn-success but_pag_nao" id="confirm_ob" onClick="ok();">CONFIRMAR</button>

0

I think it would be easier if you showed what you are using, if it is Jquery, or things like... Well using pure JS we can do so:

var primeiroBtn = "";

function ok(elemento){
  primeiroBtn = elemento.id;
}

function cancel(){
  window.alert(primeiroBtn);
}
<button onClick="ok(this);" id="testeBTN">OK btn</button>
<button onClick="cancel();">Cancel bnt</button>

Browser other questions tagged

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