Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

184
Visualizações
Sending dynamic variable to jquery

I have a table that is built dynamically with EJS template. I want to pass a dynamic value to a jQuery function when the user clicks on the button.

This is how I'm building my table:

<% for(var i=0; i< automated_campaigns.length; i++) { %>

...

<%= automated_campaigns[i].name %>

<td>
<button type="button" class="btn btn-danger btn" id="load" data-loading-text="<i class='fa fa-circle-o-notch fa-spin'></i>">Activate</button>

</td>

...
<% } %>

And this is my jQuery function that handles the click event:

<script type="text/javascript">

$('.btn').on('click', function(automated_campagin_name) {
    var $this = $(this);
  $this.button('loading');

    $.get( "/automated-campaigns/change_status/" + str(automated_campaign_name), function( data ) {
      $( ".result" ).html( data );
      alert( "Load was performed." );
    });


    setTimeout(function() {
       $this.button('reset');
   }, 8000);
});

</script>

How can I send the right campaign_name?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Put the campaign_name in a data-campaign_name since you are already looping for each of them then access it in jQuery.

$('.someElement').data('campaign_name');
over 4 years ago · Santiago Trujillo Relatório

0

You can set data-attribute on every button you need:

<button type="button" data-cname="<%= automated_campaigns[i].name %>" class="btn btn-danger btn" id="load" data-loading-text="<i class='fa fa-circle-o-notch fa-spin'></i>">Activate</button>

Script can looks like this:

 <script type="text/javascript">

$('.btn').on('click', function(automated_campagin_name) {
    var $this = $(this);
    var cname = $this.data("cname");
  $this.button('loading');

    $.get( "/automated-campaigns/change_status/" + str(cname), function( data ) {
      $( ".result" ).html( data );
      alert( "Load was performed." );
    });


    setTimeout(function() {
       $this.button('reset');
   }, 8000);
});

</script>
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda