Friday, 28 April 2017

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?



via Filipe Ferminiano

No comments:

Post a Comment