I have input section with add/delete button, that adds extra input fields. I am trying to get input field appear with a transition effect instead of popping up instantly.
I tried to use transition
but it didn’t helped. Is it possible to do it via pure CSS?
Code
JavaScript
x
18
18
1
var i = 1;
2
3
$('#add_patient').click(function() {
4
i++;
5
$('#tbl_patient').append('<tr id="row' + i + '" class="dynamic-added">
6
<td>
7
<input type="text" id="patient' + i + '" name="patient" placeholder="Patient Name" required>
8
</td>
9
<td>
10
<button class="btn_remove" type="button" name="remove" id="' + i + '">--</button>
11
</td>
12
</tr>');
13
});
14
15
$(document).on('click', '.btn_remove', function() {
16
var button_id = $(this).attr("id");
17
$('#row' + button_id + '').remove();
18
});
JavaScript
1
3
1
td {
2
transition: 0.5s ease 1s;
3
}
JavaScript
1
18
18
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
<table id="tbl_patient">
3
<thead>
4
<tr>
5
<th colspan="2" class="uk-text-center">Patient</th>
6
</tr>
7
</thead>
8
<tbody>
9
<tr>
10
<td>
11
<input type="text" id="patient" name="patient" placeholder="Patient Name">
12
</td>
13
<td>
14
<button type="button" name="add_patient" id="add_patient">+</button>
15
</td>
16
</tr>
17
</tbody>
18
</table>
Advertisement
Answer
I don’t use jQuery so had to use a little vanilla Javascript but essentially the transition is applied to the table-row and uses opacity
from 0 – 1 to create the fade-in effect.
JavaScript
1
22
22
1
var i = 1;
2
3
$('#add_patient').click(function() {
4
i++;
5
$('#tbl_patient').append('<tr id="row' + i + '" class="dynamic-added hidden">
6
<td>
7
<input type="text" id="patient' + i + '" name="patient" placeholder="Patient Name" required>
8
</td>
9
<td>
10
<button class="btn_remove" type="button" name="remove" id="' + i + '">--</button>
11
</td>
12
</tr>');
13
14
setTimeout(()=>{
15
document.querySelector('tr.hidden:last-of-type').classList.remove('hidden');
16
},100);
17
});
18
19
$(document).on('click', '.btn_remove', function() {
20
var button_id = $(this).attr("id");
21
$('#row' + button_id).remove();
22
});
JavaScript
1
7
1
tr{
2
transition:0.5s ease-in-out all;
3
opacity:1;
4
}
5
.hidden{
6
opacity:0;
7
}
JavaScript
1
18
18
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
<table id="tbl_patient">
3
<thead>
4
<tr>
5
<th colspan="2" class="uk-text-center">Patient</th>
6
</tr>
7
</thead>
8
<tbody>
9
<tr>
10
<td>
11
<input type="text" id="patient" name="patient" placeholder="Patient Name">
12
</td>
13
<td>
14
<button type="button" name="add_patient" id="add_patient">+</button>
15
</td>
16
</tr>
17
</tbody>
18
</table>