diff options
author | Bharat Mediratta <bharat@menalto.com> | 2009-09-15 20:21:12 -0700 |
---|---|---|
committer | Bharat Mediratta <bharat@menalto.com> | 2009-09-15 20:21:12 -0700 |
commit | 8b7e3e6c1a6a708b7b60837c33a895682c7de32f (patch) | |
tree | d92994cfccd40bbbb7ba3db41fb5088461cb898e | |
parent | add3bed2750f74edb6586f5379c135245ea428bd (diff) |
Refactor all the short form code to tighten it up, and make it such
that the form button is disabled if we're showing the placeholder
text.
-rw-r--r-- | lib/gallery.form.js | 42 |
1 files changed, 20 insertions, 22 deletions
diff --git a/lib/gallery.form.js b/lib/gallery.form.js index e1a24290..67245e60 100644 --- a/lib/gallery.form.js +++ b/lib/gallery.form.js @@ -3,47 +3,45 @@ * * @param shortForms array Array of short form IDs */ -function handleShortFormEvent(shortForms) { - for (var i in shortForms) { - shortFormInit(shortForms[i]); +function handle_short_form_event(short_forms) { + for (var i in short_forms) { + short_form_init(short_forms[i]); } } /** * Initialize a short form. Short forms may contain only one text input. * - * @param formID string The form's ID, including # + * @param form_id string The form's CSS id */ -function shortFormInit(formID) { - $(formID).addClass("gShortForm"); +function short_form_init(form_id) { + var form = $(form_id); + form.addClass("gShortForm"); - // Get the input ID and it's label text - var labelValue = $(formID + " label:first").html(); - var inputID = "#" + $(formID + " input[type=text]:first").attr("id"); + var label = form.find("label:first"); + var input = form.find("input[type=text]:first"); + var button = form.find("input[type=submit]"); // Set the input value equal to label text - if ($(inputID).val() == "") { - $(inputID).val(labelValue); + if (input.val() == "") { + input.val(label.html()); + button.enable(false); } - // Attach event listeners to the input - $(inputID).bind("focus", function(e){ - var eLabelVal = $(this).siblings("label").html(); - var eInputVal = $(this).val(); - + input.bind("focus", function(e) { // Empty input value if it equals it's label - if (eLabelVal == eInputVal) { - $(this).val(""); + if ($(this).val() == label.html()) { + $(this).val(""); } + button.enable(true); }); - $(inputID).bind("blur", function(e){ - var eLabelVal = $(this).siblings("label").html(); - + input.bind("blur", function(e){ // Reset the input value if it's empty if ($(this).val() == "") { - $(this).val(eLabelVal); + $(this).val(label.html()); + button.enable(false); } }); } |