css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div id='myTest'>
    <div class='help-link'>
        <a href='https://api.qunitjs.com/' target='_blank' rel='noreferrer'>Link to QUnit API</a>
    </div>
    <div id='qunit'></div>
    <div id='qunit-fixture'></div>
</div>

<div id='myPage'>
    <div data-test-my-instructions id='myInstructions'>
        You can submit the form to add a table row.
    </div>

    <div id='myContainer'>
        <table data-test-my-table id='myTable'>
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Description</th>
                </tr>
            </thead>

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

        <form data-test-my-form id='myForm'>
            <fieldset class='form-fieldset'>
                <legend>Primary Information</legend>

                <p data-test-my-form-item='name' class='form-item'>
                    <label for='name'>Name: <abbr title='required'>*</abbr></label>
                    <input id='name' type='text' required/>
                </p>

                <p data-test-my-form-item='description' class='form-item'>
                    <label for='description'>Description:</label>
                    <input id='description' type='text'/>
                </p>

                <input data-test-my-submit-button class='submit-button' type='submit' value='Submit'/>
            </fieldset>
        </form>
    </div>
</div>
            
          
!
            
              #myInstructions {
    margin-bottom: 24px;

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

#myContainer {
    display: flex;

    #myTable {
        th, td {
            border-bottom: 1px solid #ddd;
            padding: 8px 12px;

            font-family: 'Arial';
        }

        th {
            background-color: #eee;

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

    #myForm {
        width: 40%;
        margin-left: 20px;

        .form-fieldset {
            background-color: #fafafa;
            font-family: 'Arial';

            .form-item {
                label, input {
                    display: block;
                    margin-bottom: 8px;
                }

                label abbr {
                    border: none;
                    text-decoration: none;
                }

                input {
                    width: 100%;
                    height: 20px;
                }
            }

            .submit-button {
                float: right;
                margin-top: 8px;
                width: 50%;

                font-size: 16px;
            }
        }
    }
}

#myTest {
    margin-bottom: 20px;

    .help-link {
        font-family: 'Arial';
        font-size: 24px;
        margin-bottom: 16px;
    }
}

#myPage {
    border: 1px solid black;
    padding: 8px;
}
            
          
!
            
              /**************************************************

    Get mock data

**************************************************/
let data = [];
let nextAvailableId = 0;

// Use this to reset what we get from the backend
const loadData = () => {
    data = [
        {
            id: 0,
            name: 'V',
            description: 'v is for victory'
        },
        {
            id: 1,
            name: 'e',
            description: undefined
        },
        {
            id: 2,
            name: 'o',
            description: undefined
        },
        {
            id: 3,
            name: 'h',
            description: 'h is for hello world, this is my first program'
        },
        {
            id: 4,
            name: 'U',
            description: 'u is for user experience'
        },
        {
            id: 5,
            name: 'y',
            description: 'y is for yo-yo'
        }
    ];

    nextAvailableId = data.length;
};

// Get the data for the first time
loadData();


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

    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);

        // Display description (an optional field)
        let cell_description = row.insertCell(1);
        let node_description = document.createTextNode(datum.description || '');

        cell_description.appendChild(node_description);
        
        // Mark the row with a data-test tag
        row.setAttribute('data-test-my-row', '');
    });
};

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


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

    Listen to form submission

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

myForm.addEventListener('submit', (event) => {
    event.preventDefault();

    // Get form item values
    const name = myForm.name.value;
    const description = myForm.description.value || undefined;

    // Pass the data to the backend, which returns a
    // success message
    const createDatum = new Promise((resolve, reject) => {
        setTimeout(() => {
            data.push({
                id: nextAvailableId,
                name,
                description
            });
            nextAvailableId++;
            
            resolve('datum successfully created');

        }, 250);
    });

    // Update the table
    createDatum.then(() => {
        displayTable(data);
    });

    // Reset the form
    myForm.name.value = '';
    myForm.description.value = '';
});


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

    Write integration tests

**************************************************/
QUnit.test('We loaded QUnit correctly.', function(assert) {
    loadData();

    const var1 = 1, var2 = 1;

    assert.ok(true, 'We like tautologies!');
    assert.equal(var1, var2, 'Here is another tautology!');

    const myInstructions = document.querySelectorAll('[data-test-my-instructions]');

    assert.equal(myInstructions.length, 1, 'We see 1 page instructions.');
    assert.equal(myInstructions[0].innerText, 'You can submit the form to add a table row.', 'The page instructions are correct.');
});


QUnit.test('We loaded the data correctly.', function(assert) {
    loadData();

    const myRows = document.querySelectorAll('[data-test-my-row]');


    // TODO #1:
    //
    // Write a test to check that we have 6 rows.
    //



    //
    // END TODO #1


});


QUnit.test('We see the form correctly.', function(assert) {
    loadData();

    const myForm = document.querySelectorAll('[data-test-my-form]');
    const myFormItems = document.querySelectorAll('[data-test-my-form-item]');
    const mySubmitButton = document.querySelectorAll('[data-test-my-submit-button]');

    assert.equal(myForm.length, 1, 'We see 1 form.');
    assert.equal(myFormItems.length, 2, 'We see 2 form items.');
    assert.equal(mySubmitButton.length, 1, 'We see 1 submit button.');


    // TODO #2:
    // 
    // Write a test to check that the 1st form item is
    // labeled 'Name' and the 2nd 'Description'.
    //



    //
    // END TODO #2


});


QUnit.test('We can submit the form.', function(assert) {
    loadData();

    const myFormItems = document.querySelectorAll('[data-test-my-form-item]');
    const mySubmitButton = document.querySelector('[data-test-my-submit-button]');

    // Fill out the form
    let item_name        = myFormItems[0].getElementsByTagName('input')[0];
    let item_description = myFormItems[1].getElementsByTagName('input')[0];

    item_name.value = 'i';
    item_description.value = 'i is for integration test';

    assert.equal(item_name.value, 'i', 'We can enter the name.');
    assert.equal(item_description.value, 'i is for integration test', 'We can enter the description.');


    // Submit the form
    mySubmitButton.click();


    // TODO #3:
    // 
    // Write a test to check that the table shows a
    // new row on the bottom.
    //



    //
    // END TODO #3


});
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console