Skip to content Skip to sidebar Skip to footer

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 tabl

Solution 1:

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');

Solution 2:

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:

<scripttype="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>

Post a Comment for "Sending Dynamic Variable To Jquery"