List and post tasks with jQuery I've written the code below to display a list of tasks from a Flask API. The form and associated .ajax method post to the API, immediately appending the data to the list of tasks. The code is working, but I'm fairly new to jquery and am curious if my code could be improved.

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Home&lt;/title&gt;
&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"&gt;
$(document).ready(function () {
    $.getJSON("/api/v1/tasks", function(result){
        $.each(result['tasks'], function(key, item){
            $("div").append('&lt;li&gt;Task:' + item.title + ' Description:' + item.description + 'Status: ' + item.done + '&lt;/li&gt;');
        });
    });
});
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div&gt;&lt;/div&gt;
&lt;p&gt;Add Task&lt;/p&gt;
&lt;form id="task-form"&gt;
    &lt;input id="title" type="text"&gt;
    &lt;input id="description" type="text"&gt;
    &lt;button type="submit"&gt;Submit&lt;/button&gt;
&lt;/form&gt;
&lt;script&gt;
$("#task-form").submit(function(e){

    e.preventDefault();

    var data = {
            'title': document.getElementById('title').value,
            'description': document.getElementById('description').value
        };

    var json = JSON.stringify(data);

    $.ajax({
        type : 'POST',
        url : '/api/v1/tasks',
        data: json,
        contentType: 'application/json;charset=UTF-8',
        success: function(result) {
            $("div").append('&lt;li&gt;Task:' + data["title"] + ' Description:' + data["description"] + 'Status: false&lt;/li&gt;');
        }
    });

});
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
Here are some improvements that can be done


Move the &lt;script&gt;s to the end of &lt;body&gt;. This way, the UI is show quickly to the end user and does not have to wait to load the scripts. Also, as the code is moved to the end of the &lt;body&gt;, all the elements are already loaded and there is no need to use ready().
Create a string of HTML and append it to the element. By this, the DOM is accessed only once for any number of items in the tasks array.
There is no need to convert the Object to String using JSON.stringify(). jQuery does this for you.
Use val() to get the value of an element. It is considered to trim the string which are entered by user.
Use unique ID on the &lt;div&gt; element and use it to select it using jQuery. $('div') will select all the &lt;div&gt; elements on the page and the results will be unexpected when there are more than one elements on page.





$.getJSON("/api/v1/tasks", function (result) {
    var html = '';
    result.tasks.forEach(function (item) {
        // Append to the string
        html += '&lt;li&gt;Task:' + item.title + ' Description:' + item.description + ' Status:' + item.done + '&lt;/li&gt;';
    });

    // Update the DOM
    $('div').append(html);
});

$("#task-form").submit(function (e) {
    e.preventDefault();

    $.ajax({
        type: 'POST',
        url: '/api/v1/tasks',
        data: {
            title: $('#title').val(),
            description: $('#description').val()
        },
        contentType: 'application/json;charset=UTF-8',
        success: function () {
            $('div').append('&lt;li&gt;Task:' + data.title + ' Description:' + data.description + 'Status: false&lt;/li&gt;');
        }
    });
});




Here's same code written using Revealing module pattern.

var taskModule = (function ($) {
    'use strict';

    // Private variables and functions
    var _apiURL = 'api/v1/tasks',
        _readTasks = function () {
            $.getJSON(_apiURL, _showTasks);
        },
        /**
         * To add new task in Database
         * @param  {Object} task Object containing task title and description
         */
        _addTask = function (task) {
            var self = this;
            $.ajax({
                url: _apiURL,
                data: task,
                success: function () {
                    _showTasks([task]);
                }
            });
        },
        _showTasks = function (tasks) {
            var taskHTML = '';

            tasks.forEach(function (task) {
                taskHTML += '&lt;li&gt;Task:' + item.title + ' Description:' + item.description + ' Status:' + (item.done || 'false') + '&lt;/li&gt;';
            });

            $('#tasksContainer').append(taskHTML);
        },
        _bindEvent = function () {
            $('#task-form').on('submit', function (e) {
                e.preventDefault();

                _addTask({
                    title: $('#title').val(),
                    description: $('#description').val()
                });
            });
        };

    // Load tasks from server
    _readTasks();

    // Bind form submit event
    _bindEvent();

    // For public Interface
    // return {
    //     publicAlias: method/Variable name
    // };
}(jQuery));