I currently have working code that inserts rows into a table. It looks like this:
<script type= "text/javascript">
$(document).ready(function() {
var new_row = '<td><input type="text" value="Type question here" /></td><td><input type="button"class="add_next" value=" + " /><input type="button" class="move_up" value=" ^ " /><input type="button" class="move_down" value=" v " /><input type="button" class="remove" value=" x " /></td>';
var q_num = 2; // question number
$('.add_next').click(function() {
console.log(this);
console.log(($('<tr>')).insertAfter($(this).closest('tr'))
.attr('id', 'Q'+q_num)
.attr('name', 'Q'+q_num)
.append(new_row)
);
q_num++;
});
});
</script>
My problem here that the variable new_row is ugly. I would like to use $.append($(”)), .attr() and other jQuery functions to create this row on-the-fly but I have not come up with a way of doing it. The best I could come up with was this:
($('<tr>')).insertAfter($(this).closest('tr'))
.append($('<td>'))
.append('<input type="text" value="Type question here" />')
.append($('<td>'))
.append('<input type="button"class="add_next" value=" + " />')
.append('<input type="button" class="move_up" value=" ^ " />')
.append('<input type="button" class="move_down" value=" v " />')
.append('<input type="button" class="remove" value=" x " />');
How do I make this same entry using jQuery instead of a block of valid HTML?
Thanks in advance!
you could do something like that:
then change your insert code to: