Get data-value value that is generated dynamically

0

I need to get the value of a data-attr that is generated from an ajax request.

The maximum I got was:

$('.dataImposto').on('click', '.ImpostodeRenda', function(){
    $(".ImpostodeRenda").html("ooi");
});

The problem is that this "TaxTime" is generated dynamically from a return of an ajax.

Ajax Return

dados = $.parseJSON(data);
$.each(dados,function (key) {
    $(".dataImposto").append(" <div class='row static-info align-reverse'> <div class='col-md-8 name '>"+dados[key].nome+" ("+dados[key].valor+"%)</div> <div i class='col-md-3 value  imposto "+dados[key].nome.replace( /\s/g, '' ) + "' data-class="+dados[key].nome.replace( /\s/g, '' )+" data-imposto="+dados[key].valor+" > </div> </div>");
});

HTML generated example

<div class="dataImposto">
  <div class="row static-info align-reverse">
    <div class="col-md-8 name "> DAS(11.50 % )
    </div>
    <div id="imposto" class="col-md-3 value DAS" data-class="DAS" data-imposto="11.50"> </div>
  </div>
  <div class="row static-info align-reverse">
    <div class="col-md-8 name ">Imposto de Renda (15.00%)</div>
    <div id="imposto" class="col-md-3 value ImpostodeRenda" data-class="ImpostodeRenda" data-imposto="15.00">
      </div>
  </div>
</div>

Look at what DAS is generated ...

I wanted to get the date-class and the date-tax of both.

Thank you in advance

    
asked by anonymous 19.10.2017 / 21:19

1 answer

2

This is very simple, you use document to reference since DOM has already been loaded.

$('#gerar').on('click', function(){
    $('body').append('<button class="classeQueTereiEmTodos" data-value="OI">Get</button>')
})

$(document).on('click', '.classeQueTereiEmTodos', function(){
   var valor = $(this).attr('data-value')
   
   alert(valor)
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><buttonid="gerar">Gerar attr</button>
    
19.10.2017 / 21:28