pass 2 parameters in button Onclick event

Asked

Viewed 5,235 times

0

I have the following code that I would like to pass the ID txtCodEmpreendedor and txtName of the fields below as parameter for the event Preening();

<script>
    function AdicionarEmpreendedor(codigo, item) {
        //código
    }
</script>

<div class="col-sm-2">
                    <label style="color:cornflowerblue">ID</label>
                </div>
                <div class="col-sm-2">
                    <input type="text" id="txtCodEmpreendedor" class="form-control" placeholder="Código" style="margin-left: -42px; width:92px;">
                </div>
                <input type="text" id="txtNomeEmpreendedor" class="form-control" placeholder="Nome Empreendedor" style="margin-left: -42px; width:273px;" />

            </div>
        </div>

        <div class="modal-footer" style="margin-top: 165px" id="divBotao">
            <button type="button" id="button" class="btn btn-secondary" data-dismiss="modal" onclick="AdicionarEmpreendedor();">Confirmar</button>
        </div>
  • Do you want to pass these ids dynamically, and they can change? Or is it something fixed?

  • any special reason? would not be more practical in Function take the values of the fields?

  • @Davidalves will be dynamic

  • @Ricardopunctual No, because I will store the information typed in localStorage in the browser and regastar in another view.

3 answers

2


If it’s a fixed pass, it’s simple:

<div class="modal-footer" style="margin-top: 165px" id="divBotao">
    <button type="button" id="button" class="btn btn-secondary" data-dismiss="modal" onclick="AdicionarEmpreendedor('txtCodEmpreendedor', 'txtNomeEmpreendedor');">Confirmar</button>
</div>

But it is more interesting, already use them directly in the function, unless you will use it in other places with other name and id.

1

<button type="button" id="button" class="btn btn-secondary adicionar_empreendedor" data-dismiss="modal">Confirmar</button>

<script>
$(document).on('click','.adicionar_empreendedor',function(){
var txtCodEmpreendedor = $("#txtCodEmpreendedor").val();
var txtNomeEmpreendedor = $("#txtNomeEmpreendedor").val();
/* SUA FUNCAO CONTINUA */
});
</script>

1

I suggest this way:

var btns = {
  gravar: document.getElementById('btn-gravar'),
  ler: document.getElementById('btn-ler')
};

var txts = ['txtCodEmpreendedor1', 'txtCodEmpreendedor2'];
var val = [];
btns.gravar.addEventListener('click', function(){
  txts.forEach(function(elem, idx){
    val.push(
      {   
        "Value" :document.getElementById(elem.toString()).value
      }
    )
  });
  
  localStorage.setItem('values', JSON.stringfy(val))
})

btns.ler.addEventListener('click', function(){
  console.log(JSON.parse(localStorage.getItem('values')));
});
<input type="text" id="txtCodEmpreendedor1" />
<input type="text" id="txtCodEmpreendedor2" />

<br>

<button id="btn-gravar">Gravar</button>
<br/>
<button id="btn-ler">Ler</button>

Obs.: If you want to use localstorage, I suggest you do it outside this snippet.

Browser other questions tagged

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