$(function() { function closeAbracadabra(element) { $element = $(element); if($element.hasClass("abracadabra-container")) { container = $element; } else { container = $element.parents(".abracadabra-container"); } value = container.find(".abracadabra-input").val(); container.siblings(".abracadabra").text(value).show(); container.remove(); } $("body").on("ajax:success", ".abracadabra-form", function(e) { closeAbracadabra(this); }) $("body").on("click", ".abracadabra-cancel", function() { closeAbracadabra(this); }); $("body").on("keydown", ".abracadabra-input", function(e) { // Press Tab to submit (same function as Enter key) if (e.keyCode == 9) { e.preventDefault(); $(this.form).submit(); } // Press Escape to cancel if (e.keyCode == 27) { e.preventDefault(); closeAbracadabra(this); } }); $(".abracadabra").on("click", function() { link = $(this); link.hide(); path = link.data("path"); attribute = link.data("attribute"); formMethod = link.data("method"); remote = ((link.data("remote") == true) ? " data-remote=\"true\"" : ""); if(remote == "") { authToken = ""; type = ""; } else { authToken = ""; type = " data-type=\"" + link.data("type") + "\""; } instanceClass = link.data("class"); inputValue = link.text().replace(/"|\\"/g, """); inputId = instanceClass + "_" + attribute; inputName = instanceClass + "[" + attribute + "]"; buttons = ""; openFormTag = "
"; hiddenMethodTags = "
" + authToken + "
"; input = ""; html = "" + openFormTag + hiddenMethodTags; html += "
" + input + "
"; html += "
" + buttons + "
"; link.after(html); }); });