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

              
                <ul class="list">
  <li class="list__item">
    <h2>アイテム</h2>
    <input type="text" value="" class="" name="sample" id="sample" placeholder="例)test">
    <p class="delete">
      <button type="button" class="js-delete-btn">削除</button>
    </p>
  </li>
</ul>

<p class="add">
  <button type="button" class="js-add-btn">追加</button>
</p>
              
            
!

CSS

              
                p {
  margin: 0;
}

input {
  padding: .8em;
  border: 1px solid #bbb;
  border-radius: 3px;
}

h2 {
  font-size: 13px;
  margin-top: 0;
}

.list {
  list-style: none;
  padding-left: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 1em;
}

.list__item {
  display: inline-block;
  padding: 1em;
  box-shadow: 0 0 3px rgba(0,0,0, .2);
  border-radius: 5px;
}

/* li.list__itemが1つの時に「削除」ボタンを非表示 */
.list__item:only-of-type .delete{
  display:none;
}

button {
  border: 0px solid transparent;
  padding: .5em .8em;
  border-radius: 3px;
}
.delete {
  margin-top: .5em;
  text-align: right;
}
.delete button {
  background-color: #ddd;
  box-shadow: 0 3px 0 #999;
  color: #444;
}

.add button {
  background-color: #2e95b5;
  box-shadow: 0 3px 0 #066c8c;
  color: #fff;
}




              
            
!

JS

              
                // アイテムの個数を制限するための
// 追加ボタンの表示/非表示function
function add() {
  if($('.list__item').length == 3) {// アイテムが3つになったら
    $('.add').fadeOut();
    // 追加ボタンを非表示
  }else{//3つじゃなかったら
    $('.add').fadeIn();
    // 追加ボタンを表示
  }
};

// 追加ボタンを押した時
$('.js-add-btn').on('click',function() {
  // ul.listの中の一番下のli.list__itemをクローン
  var clone = $(this).parent().prev('ul.list').find('li.list__item:last-of-type').clone(true);
  // クローンした要素のinputのvalue属性を空にする
  clone.find('input[type="text"]').val('');
  // クローンしてinputを操作した要素をul.listの一番下に追加
  clone.appendTo('.list');
  
  // 追加ボタンの表示/非表示functionを呼び出す
  add();
});

// 削除ボタンを押した時
$('.js-delete-btn').on('click',function() {
  if($('.list__item').length > 1 ) {//li.list__itemが1つより多い時
  // 削除の確認
    var deleteConfirm = confirm('削除してよろしいでしょうか?');
    if(deleteConfirm == true) {// OKを押したら
      // 削除ボタンをクリックしたli.list__itemを削除
      $(this).closest('.list__item').remove();
    }
    
    // 追加ボタンの表示/非表示functionを呼び出す
    add();
  }
});
              
            
!
999px

Console