Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify attribute ROWSPAN with jQuery?

<table border="1">
    <tr><td>111</td><td>22</td><td rowspan="3">ads</td></tr>
    <tr><td>111</td><td class="remove">22</td></tr>
    <tr><td>111</td><td class="remove">22</td></tr>
    <tr><td>111</td><td>22</td><td rowspan="3">ads</td></tr>
    <tr><td>111</td><td class="remove">22</td></tr>
    <tr><td>111</td><td class="remove">22</td></tr>
    <tr><td>111</td><td>22</td><td rowspan="3">ads</td></tr>
    <tr><td>111</td><td class="remove">22</td></tr>
    <tr><td>111</td><td class="remove">22</td></tr>
</table>

$('.remove').click(function(){
   $(this).parent().remove();
})

FIDDLE: http://jsfiddle.net/r5BDW/1/

If I remove TR then table is breaks because ROWSPAN is too large. Is possible modify ROWSPAN? If yes, how?

like image 560
Jack Hendersen Avatar asked Mar 13 '12 01:03

Jack Hendersen


2 Answers

 $('.remove').click(function(){
     $(this).parent()
            .prevAll('tr:has(td[rowspan]):first')
            .find('td[rowspan]')
            .attr('rowspan', function(i, rs) { return rs - 1; })
            .end()
            .end()
            .remove();
 });
  • .parent()
  • .prevAll()
  • element-selector
  • has-selector
  • has-attribute-selector
  • first-selector
  • .find()
  • .attr()
  • .end()
  • .remove()
like image 200
user1106925 Avatar answered Oct 10 '22 18:10

user1106925


I would expect the attr() method to work... try this:

$('selector for the element you want to modify').attr('rowspan', 'newvalue');

For example:

$('#myCell').attr('rowspan', '2');
like image 34
joshuahealy Avatar answered Oct 10 '22 19:10

joshuahealy