I want to change the button when it's clicked. Initial there's only one "Edit" button. After I click it, it will become a "Save" button and I also want to show a "Cancel" button next to it. How can I do that? I have the code below.
<!DOCTYPE html>
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<meta charset=utf-8 />
<title>demo by roXon</title>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<button data-text="Save">Edit</button>
<p>Hello</p>
<p style="display: none">Good Bye</p>
<script>
$("button").click(function(){
$(this).nextUntil('button').toggle();
var btnText = $(this).text();
$(this).text( $(this).data('text') );
$(this).data('text', btnText );
});
</script>
</body>
</html>
I suggest a layout and jQuery like this jsFiddle example.
jQuery
HTML
CSS
you can add a new button for the cancel and just hide it as you need. you can follow the demo here
here is the code in case you need it: