Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the edit button into save button and add cancel button? [closed]

I want it where the edit button appears and then when I clear the edit button, the 'edit' will change into 'save' and then the 'cancel' button will also be shown.

$('#edit').click(function() {
  $(this).hide();
  $('#save, #cancel').show();
});

$('#cancel').click(function() {
  $('#edit').show();
  $('#save, #cancel').hide();
});
.save,
.cancel {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
  <button type="button" class="btn btn-secondary" value="edit">Edit</button>
  <button type="submit" class="btn btn-success" value="save">Save Changes</button>
  <button type="button" class="btn btn-secondary" value="cancel">Cancel</button>
</form>
like image 336
Erfamae Fermo Avatar asked Dec 15 '25 03:12

Erfamae Fermo


1 Answers

You did small mistakes in your code.

  • .save, .cancel it should be added in relevant field
  • add id for each button, because you used "id" selector for jQuery

$('#edit').click(function() {
		$(this).hide();
		$('#save, #cancel').show();
	});

	$('#cancel').click(function() {
		$('#edit').show();
		$('#save, #cancel').hide();
	});
	
	$('#save').click(function() {
		$(this).hide();
		$('#cancel').hide();
		$('#edit').show();
	});
button.save, button.cancel {
        display: none;
    }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" />
<form>
   <button type="button" class="btn btn-secondary" value="edit" id="edit">Edit</button>
   <button type="submit" class="btn btn-success save" value="save" id="save" >Save Changes</button>
   <button type="button" class="btn btn-secondary cancel" value="cancel" id="cancel" >Cancel</button>
</form>
like image 176
Libin Prasanth Avatar answered Dec 16 '25 17:12

Libin Prasanth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!