I'm building a page where users can place multiple containers with input fields that can be edited in place. My script at the moment allows me to edit in place the input fields on click but I'm running into 2 issues:
I need to edit each form individually. At the moment when click on Edit all of the fields in the other containers become editable as well.
When click cancel nothing should be saved if anything was typed.
JQuery
var readonly = true;
$(".edit").on("click", function(e) {
$('input[type="text"]').attr("readonly", !readonly);
readonly = !readonly;
$(".edit").hide();
$(".button-group").show();
});
$(".save, .cancel").on("click", function() {
$(".button-group").hide();
$(".edit").show();
$('input[type="text"]').attr("readonly", !readonly);
readonly = !readonly;
});
Thank you!
You need to target the parent of the parent element from
this
then you can scope your elements correctly. Move.cancel
to its own listener, and then share the code to close the inputs for both.cancel
and.save
listeners.You also don't need to keep the
readonly
attribute. You can simply remove it. See below for full example.JS Fiddle: https://codepen.io/anon/pen/zLWLXM