Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Details

Privacy

Go PRO Window blinds lowered to protect code. Code Editor with window blinds (raised) and a light blub turned on.

Keep it secret; keep it safe.

Private Pens are hidden everywhere on CodePen, except to you. You can still share them and other people can see them, they just can't find them through searching or browsing.

Upgrade to PRO

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

Template

Make Template?

Templates are Pens that can be used to start other Pens quickly from the create menu. The new Pen will copy all the code and settings from the template and make a new Pen (that is not a fork). You can view all of your templates, or learn more in the documentation.

Template URL

Any Pen can act as a template (even if you don't flip the toggle above) with a special URL you can use yourself or share with others. Here's this Pen's template URL:

Screenshot

Screenshot or Custom Thumbnail

Screenshots of Pens are shown in mobile browsers, RSS feeds, to users who chose images instead of iframes, and in social media sharing.

This Pen is using the default Screenshot, generated by CodePen. Upgrade to PRO to upload your own thumbnail that will be displayed on previews of this pen throughout the site and when sharing to social media.

Upgrade to PRO

HTML

              
                <div id='myInstructions'>
    You can click on the table headers to sort.
</div>

<div id='mySearchBar'>
    <label for='myInput'>Search in Description: </label>
    <input id='myInput' type='text' onkeyup='searchDescription();' autofocus/>
</div>

<table id='myTable'>
    <thead>
        <tr>
            <th title='Click to sort by name' onclick='sortBy("name");'>Name</th>
            <th title='Click to sort by description' onclick='sortBy("description");'>Description</th>
            <th title='Click to sort by child ID' onclick='sortBy("child.id");'>Child ID</th>
        </tr>
    </thead>

    <tbody id='myTableBody'>
        <!-- Will be dynamically generated -->
    </tbody>
</table>
              
            
!

CSS

              
                #myInstructions {
    margin-bottom: 24px;

    color: steelblue;
    font-family: 'Arial';
    font-size: 24px;
    font-weight: bold;
}

#mySearchBar {
    margin-bottom: 24px;

    label {
        font-family: 'Arial';
        font-size: 16px;
        font-weight: bold;
    }

    input {
        width: 240px;
        font-size: 16px;
    }
}

#myTable {
    th, td {
        border-bottom: 1px solid #ddd;
        padding: 8px 12px;
        
        font-family: 'Arial';
    }

    th {
        background-color: #eee;

        &:hover {
            background-color: #ddd;
            cursor: pointer;
        }
    }
}
              
            
!

JS

              
                /**************************************************

    Get mock data

**************************************************/
// `data` is what we get from the backend.
// `dataFiltered` is what we display on the table.
const data = [
    {
        id: 0,
        name: 'V',
        description: 'v is for victory',
        child: {
            id: 'A273',
            name: 'dog'
        }
    },
    {
        id: 1,
        name: 'e',
        description: undefined,
        child: {
            id: 'A48',
            name: 'cat'
        }
    },
    {
        id: 2,
        name: 'o',
        description: undefined,
        child: {
            id: 'B415',
            name: 'dodo'
        }
    },
    {
        id: 3,
        name: 'h',
        description: 'h is for hello world, this is my first program',
        child: {
            id: 'B30',
            name: 'bison'
        }
    },
    {
        id: 4,
        name: 'U',
        description: 'u is for user experience',
        child: {
            id: 'A706',
            name: 'bear'
        }
    },
    {
        id: 5,
        name: 'y',
        description: 'y is for yo-yo',
        child: {
            id: 'B261',
            name: 'alligator'
        }
    }
];

// Initially, display all data
let dataFiltered = data.slice();


/**************************************************

    Update the table

**************************************************/
let myTableBody = document.getElementById('myTableBody');

const displayTable = (data) => {
    myTableBody.innerHTML = '';

    data.forEach((datum, index) => {
        let row = myTableBody.insertRow();

        // Display name
        let cell_name = row.insertCell(0);
        let node_name = document.createTextNode(datum.name);

        cell_name.appendChild(node_name);


        // TODO #1:
        // 
        // Based on the code above, let's display the
        // `description` and `child's id`, too.
        // 
        // Note, because description is an optional field,
        // some data may have a value of `undefined`. For
        // these descriptions, show an empty string instead.
        //



        //
        // END TODO #1


    });
};

// Display the table for the first time
displayTable(dataFiltered);


/**************************************************

    Search data by description

**************************************************/
let myInput = document.getElementById('myInput');

const searchDescription = () => {
    // Get the query string
    const query = myInput.value.trim();


    // TODO #2:
    // 
    // Set `dataFiltered` to be a filtered array of `data`.
    // 
    // `dataFiltered` contains all data whose description
    // includes `query`.
    // 
    // If the query is empty, then display all data again.
    // 



    //
    // END TODO #2


    // Update the table
    displayTable(dataFiltered);
};


/**************************************************

    Allow sorting data

**************************************************/
const sortDataBy = (data, sortColumn) => {
    switch (sortColumn) {
        case 'name': {
            // Sort in ascending order
            data.sort((a, b) => {
                const value1 = a.name.toLowerCase();
                const value2 = b.name.toLowerCase();

                if (value1 > value2) {
                    return 1;
                }

                if (value2 > value1) {
                    return -1;
                }

                return 0;
            });

            break;
        }

        case 'description': {
            // TODO #3 (option 1):
            // 
            // Implement sorting by description (ascending
            // order only).
            // 
            // In ascending order, we want to show data
            // whose description is `undefined` or an
            // empty string **last**.
            //



            //
            // END TODO #3 (option 1)

            break;
        }

        case 'child.id': {
            // TODO #3 (option 2):
            // 
            // Implement sorting by child's id (ascending
            // order only).
            // 
            // The id will always consist of 1 letter,
            // followed by a series of numbers.
            // 
            // Here, ascending means, we sort by the letter
            // in ascending order first, then by the numbers
            // in ascending order.
            //



            //
            // END TODO #3 (option 2)

            break;
        }
    }

    // Update the table
    displayTable(data);
};

const sortBy = (sortColumn) => {
    // Sort the filtered list
    sortDataBy(dataFiltered, sortColumn);
};
              
            
!
999px
What do you call a two-legged ghost cow? Boolean Beef.

Console