How do I bind events to dynamically created elements and pass parameters?

7

I have a table that is generated via and a certain column generates a link. As it was implemented, it currently writes a with intrusive. Something like below:

return "<a href=\"#\" id=\"detalhe-nota-" + record.data.id + "\" class=\'modal-detalhe-nota\' onclick=\"exibirModalDetalheNota(" + record.data.id + ")\">" + value + "</a>";

The generated exits something like:

<a href="#" id="detalhe-nota-80549984" onclick="exibirModalDetalheNota(80549984)">23628</a>

I would like to bind the click event to this link by its id attribute, but since the id is dynamic I do not know how to do it. An alternative with would use the # modal-detalhe-nota , as below:

$(document).on("a.modal-detalhe-nota", "click", function(e) { });

As I understand the elements are dynamically created, they should have their events linked in this way and not as below:

$("a.modal-detalhe-nota").on("click", function(e) {});

I have two questions:

  • How to link the event with the id attribute?
  • How to pass a parameter to the function? For this id that is dynamically generated is a parameter.
asked by anonymous 25.11.2015 / 19:34

3 answers

1

Using your workaround is possible. These IDs can be added with an attribute of type data-* where * must be replaced with a name.

Modify the JavaScript link:

return '<a href="#" data-detalhe-nota="' +
  record.data.id + '" class="modal-detalhe-nota">' +
  value + '</a>';

jQuery has the function .data() that accesses the values of the data-* attributes directly, so you do not need to pass as a parameter.

$(document).on("a.modal-detalhe-nota", "click", function(e) {
  var id = $(this).data('detalhe-nota'); // obter o ID
});
    
25.11.2015 / 19:54
1

In jQuery it is possible to assign events to a newly created element, even though it has not yet been entered in body .

What I usually do is create this element and, at the same time, already assign its event by jQuery . Then I can add it to HTML.

Example:

function criaLinkDinamico(callback, i) {

  var $link = $('<a/>', {
               'class' : 'minha-classe',
               'id' : 'meu-id-dinamico' + i
             });

 return $link.click(callback);

}

From here you could do something like:

 var links = [];

 for (var i = 1; i < 10; i++) {

    var link = criaLinkDinamico(function ()
    { 
       console.log($(this).attr('id'));

    }, i));

    links.push(link);
 }

 $('html').append(links);

In need of passing a parameter to an element, you can encapsulate the callback.

Small example:

function criarCallbackParametro(nome)
{
  return function (e) {

    e.preventDefault();

    $('body').append(nome);
  }
}


$(function()
{
    $('#el-1').click(criarCallbackParametro('Wallace'));
  
    $('#el-2').click(criarCallbackParametro('Miguel'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><buttonid="el-1">add Wallace</button>
<button id="el-2">add Miguel</button>
 $('.meu_elemento').click(criarCallbackParametro('wallace'));

 $('.meu_elemento').click(criarCallbackParametro('miguel'));
    
25.11.2015 / 19:42
1

Using everything you already have just performing% dynamic% by .on :

$('a[id^="detalhe-nota-"]').on('click', function(){
    var id = $(this).attr('id');
    var numberId = id.replace(/\D/i, '');
    callfunction(numberId);
});
    
25.11.2015 / 20:34