I got a JS method that changes depending on the state of the button, defined by the indexOf("..some text..")
$('#add1').click(function(){
if($(this).text().indexOf("Add Me!")) {
$.ajax({
type: 'post',
url: '{{ URL('schedulizer/add') }}',
data: {
"class": ["Saab", "Volvo", "BMW"],
_token: "{{ csrf_token() }}"
},
dataType: 'json'
}).done(function(data){
... do stuff ...
});
$(this).removeClass('btn-material-yellow-600');
$(this).addClass('btn-danger');
$(this).text('Remove Me!');
return false;
} else if($(this).text().indexOf("Remove Me!")){
$.ajax({
type: 'post',
url: '{{ URL('schedulizer/remove') }}',
data: {
"class": ["Saab", "Volvo", "BMW"],
_token: "{{ csrf_token() }}"
},
dataType: 'json'
}).done(function(data){
... do stuff ...
});
$(this).removeClass('btn-danger');
$(this).addClass('btn-material-yellow-600');
$(this).text('Add Me!');
return false;
}
});
What works:
When I click the initial "Add Me!", it would change to "Remove Me!". However, when the state of the button is in "Remove Me!", it seems the conditional is not satisfied, and the state stays in "Remove Me" no matter how many times I press the button.
Apologies for the messy and redundant code.. I'm very very new to JS, so everything is a muck fest.