How do I grab this id?

I need to grab the id of this query output; that is, I need to get the delete button to grab its associated id so it’ll delete the correct row. When the results are output, there is a delete button for each row. But clicking on the delete button has no effect. (This works in Chrome, not FF.) I’m using:

      function renderResults(tx, rs) {
        e = $('#previous_scores');
        e.html("");
        for(var i=0; i < rs.rows.length; i++) {
          r = rs.rows.item(i);
          e.html(e.html() + '<button id="button_delete" onClick="deleteScores(this.id)">Delete</button> id: ' + r['id'] + ', hole ' + r['hole_num'] + ' strokes: <strong>' + r['num_strokes'] + ',</strong> email: ' + r['email'] + '<br><br>');
        }
      }

I also tried:
onClick="deleteScores(' + r['id'] + ')">

It should call:

      function deleteScores(id) {
        db.transaction(function(tx) {
            tx.executeSql('DELETE FROM Strokes WHERE course_id = id');
          });
        renderScores();
        }

At the bottom of the page I have:

    <script>
    var el = document.getElementById("button_delete");
    if(el){
        el.addEeventListener("click", deleteScores);
        }
    </script>

The DB is composed of:

      function insertScore(hole_num, num_strokes, course_id, email) {
       db.transaction(function(tx) {
          tx.executeSql('INSERT INTO Strokes (course_id, hole_num, num_strokes, email) VALUES (?, ?, ?, ?)', [course_id, hole_num, num_strokes, email]);
       });
      }

Codepen:
http://codepen.io/iPhoneDevLog/pen/vXrAmg

I do this:

function deleteScores(id) {
    db.transaction(function(tx) {
        tx.executeSql('DELETE FROM Courses WHERE course_id = ?', [id], successCB, errorCB);
        alert("id = " + id);
    });
    renderScores();
}

function successCB() {
    alert("success!");
}

function errorCB(err) {
    alert("Error processing SQL: " + err);
}

The successCB returns the correct id number for the button, but the errorCB returns: “Error processing SQL: [object SQLTransaction]”

The row is not deleted. It looks like it is grabbing the id correctly, but not executing the delete query correctly. What’s wrong with the delete query?

First thing that I notice is the different table names in your two posts. Is it Strokes or Scores?

And this is not very useful? Doesn’t that error object contain some more details, like an error description?

Two tables are used. I am adapting the code found here: http://www.mobilehtml5.com/post/401111526/tutorial-your-first-mobile-html5-app-offline

The error message is exactly what is being returned by the system. As usual, not very informative. I copy/pasted the error into Google for help and didn’t get much. It’s a pretty general description that covers a variety of situations.

I made a change to the delete query. Instead of course_id, I used id, and now it works:

function deleteScores(id) {
	db.transaction(function(tx) {
		tx.executeSql('DELETE FROM Strokes WHERE id = ?', [id], successCB, errorCB);
		alert("id = " + id);
	});
}
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.