Skip to content

Instantly share code, notes, and snippets.

@wesen
Created August 31, 2011 14:25
Show Gist options
  • Select an option

  • Save wesen/1183668 to your computer and use it in GitHub Desktop.

Select an option

Save wesen/1183668 to your computer and use it in GitHub Desktop.
/***************************************************************************
*
* 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