0
0

More than 1 year has passed since last update.

[Java Script] ボタンをクリックするとdivを追加・削除する

Last updated at Posted at 2022-12-14

Code

<div id="row-box">
    <div class="d-flex">
        <div class="form-group">
            <label>method</label>
            <input type="text" class="form-control" name="title" autocomplete="off" />
        </div>
        <div class="form-group">
            <label>condition</label>
            <input type="text" class="form-control" name="title" autocomplete="off" />
        </div>
        <div class="form-group">
            <label>value</label>
            <input type="text" class="form-control" name="title" autocomplete="off" />
        </div>
    </div>
</div>


var count = 0

$('#add-row').click(() => {
    count = count + 1
    $('#row-box').append(`
                    <div class="d-flex index-${count}">
                        <div class="form-group ">
                            <label>method</label>
                            <input type="text" class="form-control" name="title" autocomplete="off" />
                        </div>
                        <div class="form-group">
                            <label>condition</label>
                            <input type="text" class="form-control" name="title" autocomplete="off" />
                        </div>
                        <div class="form-group">
                            <label>value</label>
                            <input type="text" class="form-control" name="title" autocomplete="off" />
                        </div>
                    </div>`)
})

$('#remove-row').click(() => {
    $(`.index-${count}`).remove()
    count -= 1
})


Screen

2022-12-14 19;22;20.gif

最初のRowは残したい
divごとのname, idを変えることができるように、countで追加したdivを区別した

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0