julio hernandez
julio hernandez

Reputation: 1

Create a table with several <tr> with different classes

I put them in context: I need to create a table that has several , whose class I want to be different. It should be clarified that I am using javascript for this and I use the append function for this.

This is the code I currently have :

function my_function() {
    $(document).ready(function() {
            var number = 0;
            var num = ++number;
            $("#my_table> tbody").append("<tr class='tr" + num + "'><td class='center'>" + "HELLO WORLD" + "</td></tr>");
        }
    })
}

When I run it the result is 1, but when I call the function again it is still 1 instead of increasing the value back to 2.

The function is being called frequently so it should increase the value again but it doesn't. Now, I need you to increase the value multiple times without only doing it once. So that the second has class tr2. Thank you if you answer or give a solution.

Upvotes: 0

Views: 70

Answers (1)

Parco
Parco

Reputation: 612

You're declaring the variable inside the function so it will be reassigned var number = 0; every time you call it, declare it outside of it and it should work as you expect it to do.

Upvotes: 2

Related Questions