$(this).parent('tr')
will not find anything. Your input
element will be inside either td
or th
. parent
only detects the immediate parent, and then compares it to the selector you select. Therefore, he will not find anything. Then you clone nothing and insert a new nothing after the old nothing. Perhaps unsurprisingly, this actually does nothing.
You need to use closest
, which finds the closest element according to the selector
var $curRow = $(this).closest('tr');
Note that you are using global variables since you are not using var
(I am fixing this in the line above), which you probably do not want to do. In addition, live
not a good function to use; use on
instead, which does the same thing more elegantly:
$('#yourTable').on('click', 'input[name="cmdAddRow"]', function() { var $curRow = $(this).closest('tr'), $newRow = $curRow.clone(true); console.log($newRow); $curRow.after($newRow); console.log('added'); });
source share