2
$(document).ready(function(){
  $('#show').click(function(){
    var totrows= $('#table').attr('rows').length;
    for(i=0; i < totrows; i++){
      //var rowid = $('#table').$(this).attr('i'); 
      alert(rowid);
    }
  });
});

In The above function when we click on hyperlink "Show" we are getting the number of rows from the "table1".I want to add the Value of 'i' to the table so that it becomes "table1""table2""table3""table4""table5".The commented line is giving me error

6
  • 1
    Can you post the HTML you have...and what you want the result to be? It would make the question/intent much clearer. Jul 17, 2010 at 16:21
  • I Want to append the value of 'i' in the forloop to the 'table'
    – Someone
    Jul 17, 2010 at 16:23
  • @Someone - Just the variable, or you want to stick text into the table, e.g. creating elements? Jul 17, 2010 at 16:26
  • @nick -I want to stick the value
    – Someone
    Jul 17, 2010 at 16:29
  • @nick i want to append the value of variable 'i' to the table .So that it becomes 'table1', 'table2'
    – Someone
    Jul 17, 2010 at 16:30

3 Answers 3

1

Following is your actual code which is buggy, I'll try to explain the odds in your program, which may help you in future coding.

 $(document).ready(function(){
  $('#show').click(function(){
    var totrows= $('#table').attr('rows').length;
    for(i=0; i < totrows; i++){
      //var rowid = $('#table').$(this).attr('i'); 
      alert(rowid);
    }
  });
});

The Above Code isn't written in right way, because

1) var totrows= $('#table').attr('rows').length;

is there any attribute(or specified) by name 'rows' in your table element ?? and why do u bother about the length of an attribue ?

2) second thing, when you are using this make sure which element it is going to refer. In your code,

   $('#show').click(function(){
      var totrows= $('#table').attr('rows').length;
       for(i=0; i < totrows; i++){
          //var rowid = $('#table').$(this).attr('i'); 
           alert(rowid);
        }
   });

    
//var rowid = $('#table').$(this).attr('i');

The above line(commented) is Invalid, it should be some thing like $(this).attr('myAttr'); and moreover it is referring to $('#show') ( this should refer to tr elements if you're going to do some stuff with tr elements

3)

for(i=0; i < totrows; i++){
      //var rowid = $('#table').$(this).attr('i'); 
      alert(rowid);
    }

In the for loop i is a counter variable and not an attribute

4) Last thing make good use of jQuery API , jQuery.com is providing it for free :)

edit : if you want me to code for you, I would do something as follows

$(document).ready(function() {
    $('#show').click(function() {
      $('#table tr').each(function(i) {
         alert('table' + (i+1));
      });
    });
});

In .each() function we pass an Index and an Element as parameters like

$(selector).each(function(Index,Element){ //do some thing });

By default the Index is initialized with 0, so if you want get your final outcome as {table1,table2...} just append the value of Index after incrementing it by 1

for more Info refer to jquery website for .each() as nick said.

0

I'm not sure of the overall intent, but it would look like this:

$(document).ready(function(){
  $('#show').click(function(){
    var totrows= $('#table').attr('rows').length;
    for(i=0; i < totrows; i++){
      var rowid = 'table' + (i+1); 
      alert(rowid);
    }
  });
});

This would alert "table1", "table2", etc...one for every length of .rows i starts at 0 so you need to add 1 to it. If you want it based on the number of <tr> elements instead of some attribute, you could use .each(), like this:

$(function(){ //short for $(document).ready(function() {
  $('#show').click(function(){
    $('#table tr').each(function(i) {
      var rowid = 'table' + (i+1); 
      alert(rowid);
    });
  });
});
0

I think you want to insert index into each row or something like this e.g.

$(document).ready(function(){
  $('#show').click(function(){

    $('#mytable').find("td").each(function(i,el){

      $(el).text($(el).text()+i)

    });
  });
});​

It works on following html

 <input type="button" id="show" value="Show">

 <table id="mytable">
   <tr><td>row</td></tr>
   <tr><td>row</td></tr>
   <tr><td>row</td></tr>
   <tr><td>row</td></tr>
 </table>

You can try this out here http://jsbin.com/apeto3

2
  • Have you seen the output of your code when you click show button more than once, the output which I got after clicking twice is the `Index' is appending to the previous text. checkout !!!
    – user372551
    Jul 18, 2010 at 11:33
  • @Samurai Jack , yes that is what it was made to do, demonstrating how text can be modified on fly, as i am not exactly clear what OP wants Jul 18, 2010 at 12:38

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.