There is a button on a page with value "button1". When pressed, it must delete itself and add new button with value "button2". When "button2" is pressed, it must delete itself and add "button1" back. Kinda infinite loop.
I know that it can be done just by changing the value of a button or at least using "detach()" function, but it is the simplest case of my problem in a website that I'm currently trying to implement where I must interchange two "div"s full of data by buttons.
So, back to the problem, the thing is while "button1" works fine, "button2" does nothing. A bit of help will be appreciated.
index.html
<!DOCTYPE html>
<html>
<head>
<script src = "http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src = "script.js" type = "text/javascript"></script>
</head>
<body>
<div id = "div1">
<input type = button id = "button1" value = "button1" />
</div>
</body>
</html>
script.js
$(document).ready (function() {
$("#button1").click (function() {
$("body").append ("<div id = 'div2'></div>");
$("#div2").append ("<input type = button id = 'button2' value = 'button2' />");
$("#div1").remove();
});
$("#button2").click (function() {
$("body").append ("<div id = 'div1'></div>");
$("#div1").append ("<input type = button id = 'button1' value = 'button1' />");
$("#div2").remove();
});
});