I have a button <button class="open" id="1">Open</button>
if I click on the button I wanted the button to change into <button class="close" id="1">Close</button>
I made some script for it
Here is the script:
$(".open").click(function(){
$ths=$(this);
var id=$(this).val();
$.ajax({
type:"POST",
url:"someurl",
dataType: "html",
data:id,
success:function(res){
$thd.html("Open");
$ths.toggleClass("open close");
}
})
});
$(".close").click(function(){
$ths=$(this);
var id=$ths.val();
$.ajax({
type:"POST",
url:"someurl",
dataType: "html",
data:id,
success:function(res){
$ths.html("Close");
$ths.toggleClass("close open");
}
})
});
When I try it, On first click it changed the <button class="open" id="1">Open</button>
into <button class="close" id="1">Close</button>
On second click I hope it changed back to this <button class="open" id="1">Open</button>
But it didn't changed to what I want. It changed to this
<button class="open" id="1">Close</button>
It only changed the class, the close text to open not.
Can someone explain why it is not working? and also how to make that button change class and its text, oh yeah also is this problem has a name?.
Thx in advance for answering!