JQuery code is not working. What am I doing wrong? Callbacks, variable scope, chaining?
<p>Basically, I have HTML5 markup with two empty divs. I want to use JQuery to populate those divs with two separate forms and 2 separate flash messages based on whether a single localStorage value exists. As I've been building it, it's been working up until the point I wrote the code for dynamically displaying a form. Can someone toy with this code and tell me what I'm missing? I'm not a JavaScript expert (but I'd like to be) - I'm stuck on a rut here. Edit: So far, fixed the issue of forms not displaying. But the flash messages do not appear either, and when a button is pressed, it doesn't store the value in localStorage anymore, it makes the form disappear and then fade back in again. :(</p>

<pre><code>&lt;!DOCTYPE html&gt;

&lt;html lang="en"&gt;
    &lt;head&gt;
        &lt;meta charset="utf-8" /&gt;
        &lt;title&gt;Shrink Once API Tools&lt;/title&gt;
        &lt;link rel="stylesheet" type="text/css" href="css/bootstrap.css"&gt;&lt;/link&gt;
    &lt;/head&gt;
    &lt;body&gt;
    &lt;div class="container-fluid"&gt;
        &lt;div class="row-fluid"&gt;
            &lt;div class="span12"&gt;
                &lt;h1 class="head-title"&gt;Shrink Once API Tools&lt;/h1&gt;
            &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class="row-fluid"&gt;
            &lt;div class="span12"&gt;
                &lt;div id="flashAlert"&gt;

                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class="row-fluid"&gt;
            &lt;div class="span12"&gt;
                &lt;div class="mainFormContent"&gt;

                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;

    &lt;!-- Include JavaScript at the very bottom for faster page loads --&gt;
    &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"&gt;&lt;/script&gt;
    &lt;!--
    Fall back on local JQuery if Google CDN version is unavailable.
    (Since most sites link the Google CDN version, it is more likely
    to already be cached by the user's browser).
    --&gt;
    &lt;script&gt;window.jQuery || document.write('&lt;script src="js/vendor/jquery-1.8.2.min.js"&gt;&lt;\/script&gt;')&lt;/script&gt;
    &lt;!-- end Fallback --&gt;
    &lt;script src="js/bootstrap.js"&gt;&lt;/script&gt;
    &lt;script&gt;
        $(function () {
            var apiKeyForm = '&lt;form&gt;'
                           + '&lt;legend&gt;Enter your API Key&lt;/legend&gt;'
                           + '&lt;input type="text" id="apiKey" class="span12" placeholder="e.g. ab12c34d5678efgh90123i45678j90k1"&gt;'
                           + '&lt;span class="help-block"&gt;You can find your access key &lt;a href="https://shrinkonce.com/index.php?menu=usercp#tools" target="blank"&gt;here.&lt;/a&gt;&lt;/span&gt;'
                           + '&lt;button type="submit" id="saveAPIKey" class="btn btn-info btn-large btn-block"&gt;Save&lt;/button&gt;'
                           + '&lt;/form&gt;';

            var apiLinkForm = '&lt;form&gt;'
                            + '&lt;legend&gt;Add a link or two... or more.&lt;/legend&gt;'
                            + '&lt;button id="add" class="btn btn-large"&gt;Add&lt;/button&gt;'
                            + '&lt;button id="remove" class="btn btn-large"&gt;Remove&lt;/button&gt;'
                            + '&lt;button id="reset" class="btn btn-large"&gt;Reset&lt;/button&gt;'
                            + '&lt;button type="submit" class="btn btn-info btn-large"&gt;Submit&lt;/button&gt;'
                            + '&lt;hr /&gt;'
                            + '&lt;div id="linkForm"&gt;'
                            + '&lt;/div&gt;'
                            + '&lt;/form&gt;';

            var i = $('#linkForm input').size() + 1;
            $('#add').click(function () {
                $('&lt;input type="text" id="inputLink' + i + '" class="shrinklink span12" placeholder="http://google.com"&gt;').fadeIn('slow').appendTo('#linkForm');
                i++;
                return false;
            });
            $('#remove').click(function () {
                if (i &gt; 1) {
                    $('.shrinklink:last').fadeOut('normal', function () { $(this).remove(); });
                    i--;
                }
                return false;
            });
            $('#reset').click(function () {
                while (i &gt; 2) {
                    $('.shrinklink:last').remove();
                    i--;
                }
                return false;
            });
            $("#saveAPIKey").click(function () {
                if (typeof $('#apiKey').val() !== null) {
                    localStorage.setItem(apiKey, $('#apiKey').val());
                    $(apiKeyForm).fadeIn('slow').appendTo('.mainFormContent');
                }
                else {
                    alert("API Key not set!");
                }
                return false;
            });

            if (localStorage.length &gt; 0) {
                $('&lt;div class="alert alert-error"&gt;You have not yet entered your API token. Add it below, and it will be persisted in memory.&lt;/div&gt;').fadeIn('slow').appendTo('.flashAlert');
                $(apiLinkForm).fadeIn('slow').appendTo('.mainFormContent');
            }
            else {
                $('&lt;div class="alert alert-success"&gt;Your API token is ' + localStorage.getItem(apiKey) + '.&lt;/div&gt;').fadeIn('slow').appendTo('.flashAlert');
                $(apiKeyForm).fadeIn('slow').appendTo('.mainFormContent');
            }
        });
    &lt;/script&gt;
&lt;/body&gt;
</code></pre>

<p></p>
