pass 2 parameters in button's OnClick event

0

I have the following code that I would like to pass the txtCodEmpreendedor and txtNameEmpreendedor IDs of the fields below as a parameter to the AddEprepresent / p>

<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>
    
asked by anonymous 04.04.2018 / 21:26

3 answers

2

If you go through a fixed way, 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's more interesting, already use them directly in the function, unless you will use it in other places with other name and id.

    
04.04.2018 / 21:32
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>
    
04.04.2018 / 21:39
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>

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

    
04.04.2018 / 22:04