I am using twitter Bootstrap with jQuery
and i was wondering how can i toggle between “Subscribe / UnSubscribe”
Each click its an ajax request so the user can Subscribe / UnSubscribe
Once a user subscribed i display a messege “Subscribed!”
What about if a user regret and want to unubscribed?
I would like to have in one button both functionality.
I am changing the id dynamically but it doent work!
button class="btn" id="subscribe" data-toggle="button" data-loading-text="Loading..." data-complete-text="Subscribed!">Subscribe
$(document).ready(function () {
$('#subscribe').button();
$('#unsubscribe').button();
});
$('#unsubscribe').click(function () {
$('#unsubscribe').button('loading');
$.ajax({
url: "<%= "#{root_url}unsubscribe_from/#{@collection.id}"%>" ,
type: "GET",
data: "",
success: function(data) {
$('#unsubscribe').button('complete');
jQuery("#unsubscribe").attr("id","subscribe");
jQuery("#subscribe").attr("data-complete-text").text("Subscribed!");
}});
return false;
});
$('#subscribe').click(function () {
$('#subscribe').button('loading');
// $('#subscribe').button('reset');
$.ajax({
url: "<%= "#{root_url}subscribe_to/#{@collection.id}"%>" ,
type: "GET",
data: "",
success: function(data) {
$('#subscribe').button('complete');
jQuery("#subscribe").attr("id","unsubscribe");
jQuery("#unsubscribe").attr("data-complete-text").text("Unsubscribe!");
}});
return false;
});
1 Answer