I am trying to add a row dynamically by clicking the add button and later i am trying to move up and down by clicking the respected buttons... In the code i amhaving 3 static rows, the data typed in the textbox goes up and down perfectly. But regarding the ROW which i have dynamically created, there is no action.
I came to know that javascript wont get action for dynamically created elements.. If so, what i have to do solve this issue...Thanks...
var html = '<tr><td>row 4</td><td><input type="text" name="uname"></td><td><input type="button" value="move up" class="move up" /></td><td><input type="button" value="move down" class="move down" /></td></tr>';
$(function() {
$('#addRow').click(function() {
$('tbody').append(html);
});
$('#mytable input.move').click(function() {
var row = $(this).closest('tr');
if ($(this).hasClass('up'))
row.prev().before(row);
else
row.next().after(row);
});
});
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<table>
<tbody id="mytable">
<tr>
<td>row 1</td>
<td><input type="text" name="uname"></td>
<td><input type="button" value="move up" class="move up" /></td>
<td><input type="button" value="move down" class="move down" /></td>
</tr>
<tr>
<td>row 2</td>
<td><input type="text" name="uname"></td>
<td><input type="button" value="move up" class="move up" /></td>
<td><input type="button" value="move down" class="move down" /></td>
</tr>
<tr>
<td>row 3</td>
<td><input type="text" name="uname"></td>
<td><input type="button" value="move up" class="move up" /></td>
<td><input type="button" value="move down" class="move down" /></td>
</tr>
</tbody>
</table>
<button id="addRow">Add</button>
</body>
</html>
I would like to add new tr with controls dynamically and later i would like to reorder the same by clicking the up and down button and then to get its values in the reordered form..