Created
August 31, 2011 14:25
-
-
Save wesen/1183668 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*************************************************************************** | |
| * | |
| * Input toggles. Previous element is replaced with input box when clicked | |
| * | |
| ***************************************************************************/ | |
| $.fn.makeInputToggle = function () { | |
| this.each(function () { | |
| var that = $(this); | |
| if (that.data("inputToggled")) { | |
| /* avoid double input toggle handling */ | |
| return; | |
| } | |
| that.hide().prev().click(function () { | |
| $(this).hide(); | |
| that.show().focus(); | |
| }); | |
| if ((that.is("input") && ((that.attr("type") === "text") || | |
| (that.attr("type") === "password"))) || | |
| (that.is("textarea"))) { | |
| /* if input is a text input, check for enter key pressed to return to normal */ | |
| that.keydown(function (l) { | |
| debugLog(l.keyCode, "keycode"); | |
| if (l.keyCode == 13) { | |
| var newVal = $(this).val(); | |
| $(this).hide().prev().text(newVal).show(); | |
| } | |
| }); | |
| } else if (that.is("select")) { | |
| /* if it's a select list, use the text value of the selected option instead of the value. */ | |
| that.change(function () { | |
| var newVal = $(this).find("option:selected").text(); | |
| $(this).hide().prev().text(newVal).show(); | |
| }); | |
| } else { | |
| /* else replace text with the value */ | |
| that.change(function () { | |
| var newVal = $(this).val(); | |
| $(this).hide().prev().text(newVal).show(); | |
| }); | |
| } | |
| that.data("inputToggled", true); | |
| }); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment