Reputation: 9225
I have the following JQuery which adds a row to a table dynamically:
var i = 0;
...
i++;
var rowId = "itemEntry" + i + "";
alert(rowId);
var strRowEntry = '<tr id="itemEntry' + i + '"><td class="rowItem">' + $("#itemChoice option:selected").text() +'</td><td class="rowItem">' + priceEach + '</td><td class="rowItem">' + $("#quantity").val() + '</td><td class="rowItem">' + $("#price").val() + '</td><td class="rowItem"><a href="#" onclick="doThis(' + rowId + ');"><img class="deleteRow" src="edit.png" border="0" id="imgEdit" /></a></td></tr>';
$('#itemTable tr:last').after(strRowEntry);
I am trying to get the row ID from the doThis()
function like this:
function doThis(p) {
alert(p);
}
Whenever the alert comes up, I get the following message:
[object HTMLTableRowElement]
What am I doing wrong? Why isn't it displaying the table ID instead?
What I am looking to do is for each row give me the value from each column on that given row...
How do I achieve that?
Upvotes: 1
Views: 78
Reputation: 700152
Your code is ending up doing this (assuming i == 42):
onclick="doThis(itemEntry42);"
The expression itemEntry42
is not a string, it evaluates to an identifier, which will be the element with id="itemEntry42"
. The parameter p
ends up being the element, not the id of the element.
You should use apostrophes around the id to make it a string literal instead of an identifier:
... onclick="doThis(\'' + rowId + '\');" ...
Upvotes: 2