0

I have some script that is delivering a series of rows from an SQL database.

I want to append.() a button to each row. My current code is:

tx.executeSql('SELECT * FROM myprogram', [], function (tx, results) {
                   var len = results.rows.length, i;
                    for (i = 0; i < len; i++) {

                        $('#myprog').append(results.rows.item(i).event)
                               $('.event').append('<button class="save_event">Remove</button>');

                        }
                });

But the buttons get doubled up. For example if I get 3 rows, the first row will have 3 buttons, the 2nd will have 2 and the last will have 1 button.

Can anyone help me out to have the buttons display only once per row?

Thanks!

2 Answers 2

0

I found the answer, quite simple:

var len = results.rows.length, i;
                    for (i = 0; i < len; i++) {

                        $('#myprog').append(results.rows.item(i).event).append('<button class="remove_event">Remove</button>');

                    }
0
$('#myprog').append(results.rows.item(i).event).append('<button class="remove_event">Remove</button>');

You need to append to $('#myprog') selector.

2
  • how do you mean cirius? #mrprog is the wrapper div that contains all the rows from the db so wouldnt it only append once?
    – MeltingDog
    Jun 21, 2012 at 0:59
  • No it won't. Because it is indexed with the i variable within the loop. The ith index will separate each button from the other. Jun 21, 2012 at 1:07

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.