Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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.

Behavior

Auto Save

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.

HTML

              
                <main class="main">
    <h1>Javascriptで作るTODOリストのサンプル</h1>

    <div class="todo-list">
        <div class="add-item-area">
            <input type="text" class="add-item"><button class="add-button" aria-placeholder="入力してください">追加する</button>
        </div>
        <div class="item-area-incomplete">
            <p class="ttl">TOTOリスト</p>
            <ul>
            </ul>
        </div>
        <div class="item-area-complete">
            <p class="ttl">完了</p>
            <ul>
            </ul>
        </div>
    </div>

</main>
              
            
!

CSS

              
                input {
    border: none;
    border-radius: 8px;
    padding: 5px 10px;
    outline: none;
}

button {
    border-radius: 8px;
    border: none;
    padding: 5px 10px;
    margin-left: 10px;
    background: #ff6565;
    color: #fff;
    cursor: pointer;
}

button.incomplete-button {
    background-color: #858585;
}

li {
    display: flex;
    align-items: center;
    padding: 10px 0;
    border-bottom: 1px dotted #ccc;
}

li p {
    margin: 0;
    flex: 0 1 820px;
}

.add-item-area {
    background: #e7e7e7;
    padding: 10px;
    margin: 20px 0;
    border-radius: 10px;
}

.item-area-incomplete {
    background: #edffe5;
    padding: 10px;
    margin: 20px 0;
    border-radius: 10px 10px 0 0;
    min-height: 200px;
}

.item-area-incomplete .ttl {
    color: #fff;
    font-weight: bold;
    border-radius: 10px 10px 0 0;
    margin: -10px;
    background: #04945d;
    padding: 10px;
}

.item-area-incomplete button {
    flex: 1 0 60px;
}

.item-area-complete {
    background: #fff3e3;
    padding: 10px;
    margin: 20px 0;
    border-radius: 10px 10px 0 0;
    min-height: 200px;
}

.item-area-complete .ttl {
    color: #fff;
    font-weight: bold;
    border-radius: 10px 10px 0 0;
    margin: -10px;
    background: #c79b49;
    padding: 10px;
}

.item-area-complete button {
    flex: 1 0 60px;
}
              
            
!

JS

              
                // クラス
class TodoList {
    // コンストラクタ
    constructor() {
            this.DOM = {};
            this.DOM.incomplete = document.querySelector(".item-area-incomplete ul");
            this.DOM.complete = document.querySelector(".item-area-complete ul");

        }
        // リストを生成
    _createItem(text) {
            const liElm = document.createElement('li');
            const pElm = document.createElement('p');
            const deleteButton = document.createElement('button');
            const completeButton = document.createElement('button');

            completeButton.classList.add('complete-button');
            completeButton.innerText = '完了';
            deleteButton.classList.add('incomplete-button');
            deleteButton.innerText = '削除';
            pElm.innerText = text;

            // 完了ボタンクリック
            completeButton.addEventListener('click', (e) => {
                this.deleteItem(e.srcElement.parentNode, this.DOM.incomplete);
                this.addCompleteItem(text);
            });
            // 削除ボタンクリック
            deleteButton.addEventListener('click', (e) => {
                const confirmation = confirm("タスクを削除しても良いですか?");

                if (confirmation) {
                    this.deleteItem(e.srcElement.parentNode, this.DOM.incomplete);
                }
            });

            // 生成した要素
            liElm.appendChild(pElm);
            liElm.appendChild(completeButton);
            liElm.appendChild(deleteButton);

            return liElm;
        }
        // 完了に追加
    _completeItem(text) {
            const liElm = document.createElement('li');
            const pElm = document.createElement('p');
            const backButton = document.createElement('button');

            backButton.classList.add('complete-button');
            backButton.innerText = '戻る';
            pElm.innerText = text;
            // 戻るボタンクリック
            backButton.addEventListener('click', (e) => {
                this.deleteItem(e.srcElement.parentNode, this.DOM.complete);
                this.addItem(text);
            });

            liElm.appendChild(pElm);
            liElm.appendChild(backButton);

            return liElm;
        }
        // リストを追加
    addItem(text) {
            this.DOM.incomplete.appendChild(this._createItem(text));
        }
        // 完了に追加
    addCompleteItem(text) {
            this.DOM.complete.appendChild(this._completeItem(text));
        }
        // リストを削除
    deleteItem(target, domparent) {
        domparent.removeChild(target);
    }
}

function addtodoEvent() {
    const addItemTxt = document.querySelector(".add-item").value;
    // 値を入力していない時は処理を終了
    if (addItemTxt == "") {
        alert("値を入力してください");
        return
    }
    // フォームの値をリセット
    document.querySelector(".add-item").value = "";
    // インスタンス化
    const totoList = new TodoList();
    totoList.addItem(addItemTxt);
}

// クリックイベント
document.querySelector(".add-button").addEventListener('click', () => {
    addtodoEvent();
});

// キーボードイベント
document.addEventListener('keypress', keypress);
const regexEnter = new RegExp('(=|Enter)');

function keypress(e) {
    // エンターが押されたら
    if (regexEnter.test(e.key)) {
        addtodoEvent();
    } else {
        return false;
    }
}
              
            
!
999px

Console