views:

32

answers:

2

Is there any way I can add a column based on another column's attribute? Something like this: If a td has attribute colspan="2", then add one new  before it. Thank you.

<table>
  <tr>
    <td>aaa</td>
    <td colspan="2">bbb</td>
    <td>ccc</td>
    <td>ddd</td>
    <td colspan="2">eee</td>
  </tr>
</table>

The result table should be:

<table>
  <tr>
    <td>aaa</td>
    <td>&nbsp; just added based on the right below column tag</td>
    <td colspan="2">bbb</td>
    <td>ccc</td>
    <td>ddd</td>
    <td>&nbsp; just added based on the right below column tag</td>
    <td colspan="2">eee</td>
  </tr>
</table>

Based on the help from Reigel and Rob, here is the complete example:

<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">
 $(document).ready(function(){
    $('td[colspan=2]').before('<td>&nbsp; just added based on the right below column    tag</td>') ;
   });
</script>

<body>
<table border="1"> 
  <tr> 
    <td>aaa</td> 
    <td colspan="2">bbb</td> 
    <td>ccc</td> 
    <td>ddd</td> 
    <td colspan="2">eee</td> 
  </tr> 
</table>
</body>
</html>
A: 
$('td[colspan=2]').before(...)
Rob
Thank you Rob. I tried your and Reigel both worked. Sorry I am allowed to mark one as answer.
David
+1  A: 

try this...

$('table td[colspan=2]').before('<td>&nbsp; just added based on the right below column tag</td>');
Reigel
How can I set up the function and wire it to the html table to make it work? Sorry for the question, but I am really have no clue on jquery, totally new to this language. Thank you.
David