Search code examples
jqueryhtml-tableattributescell

Moving an id from cell to relative cell in a table using jQuery


I want to "move" an id from one cell in a table to a relative cell in the table.

To simplify things, let's say I have this:

<tr><td></td><td id='a'></td><td></td><td></td><td></td></tr>

One of the cells has id='a' but I don't know which it will be. I simply want to make the one to the right of it have id='a'.

Getting the cell with id='a' and un-setting the id is simple enough:

var cell_a = $('#a');
cell_a.attr('id','');

However, I'm not sure how to get the cell to the right. I tried a few things with cell_a.parent().children() and cell_a.parent().find() but wasn't able to just pull out a list. Once I get the one to the right it will obviously be something like:

var new_cell_a = ?
new_cell_a.attr('id','a');

Solution

  • Try cell_a.next()

    like explained in this page: http://api.jquery.com/next/