1

I have one table that has 2 row and 3 column. I want add 2 attribute to any td that detect place it. (these attributes are col & row that show column and row for any tds.)

this is my code :

<table id="tab">
  <tr id="rows">
    <td></td>
    <td></td>
    <td></td>
  </tr>

  <tr id="rows" style="top:50px">
    <td></td>
    <td></td>
    <td></td>
  </tr>
</table>

and I want use loop that add 2 attribute in it any level for any td and at last I want to get this code but I cannot do it:

<table id="tab">
  <tr id="rows">
    <td col="1" row="1"></td>
    <td col="2" row="1"></td>
    <td col="3" row="1"></td>
  </tr>

  <tr id="rows" style="top:50px">
    <td col="1" row="2"></td>
    <td col="2" row="2"></td>
    <td col="3" row="2"></td>
  </tr>
</table>
3
  • What are you having problems with? What isn't working? Commented Jul 28, 2013 at 6:35
  • my problem is I don't know how add attr to tag Commented Jul 28, 2013 at 6:36
  • $(elem).attr('foo', 'bar') Commented Jul 28, 2013 at 6:37

2 Answers 2

8

You can do it by this jQuery:

$("#tab tr td").each(function(){
    $(this)
        .attr("col", $(this).index() + 1)
        .attr("row", $(this).parent().index() + 1);
});
Sign up to request clarification or add additional context in comments.

Comments

3

documentation will help

$('selector').attr('name', 'value');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.