Reputation: 1381
I have a table on my page like this:
<table border="1">
<tr>
<td>first</td>
<td><a href="#">first click</a></td>
</tr>
<tr>
<td>second</td>
<td><a href="#">second click</a></td>
</tr>
<tr>
<td>third</td>
<td><a href="#">third click</a></td>
</tr>
<tr id="change">
<td colspan="2">
<button>change</button>
</td>
</tr>
</table>
When I click on of the links, I want the move '#change' under the clicked link's . For example; when I click on "first click" table changes like this..
<table border="1">
<tr>
<td>first</td>
<td><a href="#">first click</a></td>
</tr>
<tr id="change">
<td colspan="2">
<button>change</button>
</td>
</tr>
<tr>
<td>second</td>
<td><a href="#">second click</a></td>
</tr>
<tr>
<td>third</td>
<td><a href="#">third click</a></td>
</tr>
</table>
How can I do it with JQuery?
Upvotes: 3
Views: 756
Reputation: 268414
Using event delegation, therefore only adding on event listener to the mix rather than several:
// Using Event Delegation only adds one event listener
$("table").on("click", "a", function (event) {
// Prevent any navigation or jumping to the top of the page
event.preventDefault();
// Find the closest <tr>, insert #change after it.
$(this).closest("tr").after($("#change"));
});
Upvotes: 1
Reputation: 9294
$("a").click( function(){
var $change = $('#change');
$('#change').remove();
$(this).closest('tr').after($change);
});
Upvotes: 0
Reputation: 1060
jQuery('td a').click(function(){
$(this).parents("tr").after($('#change'));
});
Upvotes: 1
Reputation: 23075
Try this:
$('table tr td a').click(function () {
$(this).closest('tr').after($('#change'));
});
Upvotes: 2
Reputation: 7887
try this (untested)
jQuery('td a').each(function(){
var j = jQuery(this);
jTr = j.closest('table').find('#change');
j.closest('tr').after(jTr);
});
Upvotes: 0