Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have created two separate buttons on both my HTMl and Jquery, i want to find a way that can make on button that would do the work to do the switch. Is this possible? Please view my code below, thanks.

What I have tried:

JavaScript
<div class="form-group"><br>
<div class="col-md-3 text-center">
<button id = "singlebutton" name="singlebutton"
class="btn btn-danger " onclick="BtnOff();">Off</button><br>
</div>
</div>
<br/>
<div class="form-group"><br>
<div class="col-md-3 text-center">
<button id = "singlebtn" name="singlebtn"
class="btn btn-success " onclick="BtnOn();">On</button><br>
 
// button click functionality here.
$('.btnOff').on('click', function(){
BtnOff();
$(this).off('click');
});
$('.btnOn').on('click', function(){
BtnOn();
$(this).off('click');
});
// functionality for button-Off.
function BtnOff() {
$.ajax({
url:'https://api.thingspeak.com/update?api_key=DLQ0F7W5FGVO1I9Q&field8=0',
type:'GET',
data:{
format:'text'
},
success:function(response) {
alert(response);
},
error:function(){
$('#error').text("There was an error processing your request.Please try again");
$('#singlebutton').append(data);
}
});
}
// functionality for On Button.
function BtnOn() {
$.ajax({
url:'https://api.thingspeak.com/update?api_key=DLQ0F7W5FGVO1I9Q&field8=1',
type:'GET',
data:{
format:'text'
},
success:function(response){
alert(response);
},
error:function(){
$('#error').text("There was an error while processing your request.Please try again");
$('#singlebtn').append(data);
}
});
}
Posted
Updated 25-Nov-19 23:01pm

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900