0

I'm trying to change the text of a button, but I'm unable to do it.

$( function() {
 $('.btn').on('click', function(automated_campagin_hash) {
    var $this = $(this);
    $this.button('loading');
        
    var automated_campagin_hash = $this.data('campaign-automated-hash');
    var url = "/automated-campaigns/change_status/" + String(automated_campagin_hash);
    $.get( url , function( data ) {
          console.log(data);
        $this.button('reset');
        $this.removeClass("btn-danger").addClass("btn-info");
        $this.html('Activated');
    });
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<button type="button" class="btn btn-danger btn" id="load" data-loading-text="<i class='fa fa-circle-o-notch fa-spin'></i>" data-campaign-automated-hash="<%= automated_campaigns[i].automated_campaign_hash %>">Desativar</button>
Tibrogargan
  • 4,508
  • 3
  • 19
  • 38
Filipe Ferminiano
  • 8,373
  • 25
  • 104
  • 174
  • No, the problem is that it not changing to Activated – Filipe Ferminiano Apr 28 '17 at 19:00
  • @Santi `$this` is a variable he defined at the top of his function and is assigned to `$(this)` – Lixus Apr 28 '17 at 19:00
  • Is it changing the class? – Barmar Apr 28 '17 at 19:00
  • Running the code in the snippet makes it pretty obvious what the problem is. – Tibrogargan Apr 28 '17 at 19:01
  • Yes, it is changing the class. The this.button I got from this snippet: http://codepen.io/jmalatia/pen/HkmaA – Filipe Ferminiano Apr 28 '17 at 19:05
  • Sorry, JQuery UI is included in my source code: And there is no error on loading it in console – Filipe Ferminiano Apr 28 '17 at 19:07
  • `.button()` is a [bootstrap thing](http://getbootstrap.com/javascript/#buttons) – Artur Filipiak Apr 28 '17 at 19:08
  • Yes, it is included too. – Filipe Ferminiano Apr 28 '17 at 19:09
  • @ArturFilipiak It's not just a bootstrap thing, jQuery UI also has a [`.button`](http://www.tutorialspark.com/jqueryUI/jQueryUI_Button_Methods.php) method. – Tibrogargan Apr 28 '17 at 19:12
  • @FilipeFerminiano It's a pity you're using off-site resources in your code - as it's difficult for someone else to reproduce the errors you're seeing, they will be getting different ones. Maybe you could work on creating a [mcve] – Tibrogargan Apr 28 '17 at 19:14
  • 1
    have you seen this: http://stackoverflow.com/questions/30190437/uncaught-error-cannot-call-methods-on-button-prior-to-initialization-attempted – Sagiv b.g Apr 28 '17 at 19:15
  • In addition to @Tibrogargan comment, your code works for me http://jsbin.com/jojuwoleca/edit?html,js,output – Shadi Shaaban Apr 28 '17 at 19:16
  • 1
    @Sag1v thanks, I changed the order of jquery ui and bootstrap plugins. I added jquery ui and then boostrap. Can you add an aswer and I accept it? – Filipe Ferminiano Apr 28 '17 at 19:22
  • This question should've been closed, rather than accepted, IMO: *"This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers."* - While this answer may be a solution to many problems out there, I dont' see this question being the one they find. – Tyler Roper Apr 28 '17 at 19:32
  • @Santi I partly disagree, you may have a point but i think that this question holds much more details then other questions on this manner, even though my answer was only referencing another question, that referenced question lacks the details it needs (code snippet?). i think the OP of this question should include the `bootstrap` link tag to the snippet and that will get this question the ability to reproduced while running the snippet and include a broader picture. – Sagiv b.g Apr 28 '17 at 20:30
  • If OP edited the entire question to warrant the answer he's accepted, then sure. But that's what "Retract Close Vote" is for. There are certainly changes that could make this question work better, however as it stands, they aren't there - thus, I believe in it's *current form*, it should be closed. – Tyler Roper Apr 28 '17 at 20:33

1 Answers1

-1

You mentioned you are using bootstrap as well (though it's not referenced in your snippet). so here is a possible solution quoted from Another SO question

It's caused by jquery-ui and bootstrap-button plugins conflict. In fact jquery-ui code js code should go before bootstrap.js, and that solves the problem.
Just put jquery-ui plugins before bootstrap.js

Community
  • 1
  • 1
Sagiv b.g
  • 30,379
  • 9
  • 68
  • 99
  • I believe it's because you're answering the question by copy/paste someone's answer without adding anything new. You should mark the question as a duplicate if it was already asked and answered. – Artur Filipiak Apr 29 '17 at 09:54