How to get button value when firing a Shown.bs.modal event

Asked

Viewed 100 times

0

Follows the code:

HTML:

<button id="meubotao"type="button" value="5">Click Me!</button>

JS:

$( "#meubotao" ).click(function() {
  $("#myModal").modal("show");
});

I also have this code to follow (triggers this event after modal to be loaded):

$('#myModal').on('shown.bs.modal', function (e) {
  // como obter valor a partir daqui...
})

The event click ("mypost"), I can get value using $(this).val(); or example of jsfiddle.

How can I get value from that button after modal to be loaded ?

  • Ever thought about putting this value in a variable? So you can use it wherever you want.

  • Yeah, I’ve thought about it, but it’s got to get direct value from the event ?

  • Ever tried to make $('#meubotao').val() after the Shown event of the modal?

2 answers

0

If I understand correctly:

$('#myModal').on('shown.bs.modal', function (e) {
    // Aqui você pode fazer o que quiser com o valor
    $('#meubotao').val();
})
  • I tried, he’s like undefined.

  • This modal loads some external content to the button page where it is called?

0


IN HTML:

We use the resource date-* :

<button id="meubotao"type="button" data-value="5">Click Me!</button>

IN THE SCRIPT:

We need to redeem the value.

//Evento de disparo para quando modal é carregada.
$('#myModal').on('shown.bs.modal', function (event) {
    //Selecionamos o botão que disparou o evento.
    var botao = $(event.relatedTarget);
    //Aqui você pega o valor.
    var valor = botao.data('value');

   //Faça o que quiser com o valor.
});

Browser other questions tagged

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