Take data-value that is generated dynamically

Asked

Viewed 652 times

0

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

The most I got was:

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

The problem is that this "Impostoderenda" is generated dynamically from an ajax return.

Return of the Ajax

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>");
});

Example of generated HTML

<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 who’s born DAS...

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

Thank you in advance

1 answer

2


That’s pretty simple, you use the document to refer since the 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>
<button id="gerar">Gerar attr</button>

  • Nice, but here’s the deal. I have a div with "timeTime" the content of the return of the ajax is placed inside that div, with the code I showed above. This ajax is sent when I press a button.

  • You want to send this value as ajax parameter?

  • No, I want to redeem the value of the data-attr that comes created with the return of ajax, within the div

  • So just do it the way I did it up. In case it didn’t work anyway, explain to me better that I help you solve.

  • It’s my time for class, is it okay to call you tomorrow? has Skype or something?

  • Came, good morning, I got to add you on Skype

  • I had to make a small adaptation but the idea of taking one that already exists for everyone worked out cool

Show 2 more comments

Browser other questions tagged

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