I have created a button on my website when it is clicked, I sent data on some php
file using ajax
and return the results. The code I am using is below,
My Goal :
- When that button is clicked for the first time. I want to send the data on some
php
file usingajax
and return the results, and - When it is clicked for the second time, I just want to hide the content, and
- When it is clicked for the third time, I just want to show the container without calling the ajax again.
jQuery:
$(function() {
$('#click_me').click(function(){
var container = $('#container').css('display');
var id = $('#id').html();
if(container == 'none'){
$.ajax({
type: 'POST',
data: {id: id},
url: "ajax/get_items.php",
}).done(function(data) {
$('#container').html(data);
}).success(function(){
$('#container').show('fast');
});
}else if(container == 'block'){
$('#container').hide('fast');
}
});
});
Html :
<input type="button" id="click_me" value="Click Me"/>
<div id="container"></div>