I have a javascript function that runs fine onclick when the td element is added via javascript. The remove button works fine. But when I create the element with php and click remove, I get:
SyntaxError: syntax error
var header = document.getElementById(
Even though the counter is working correctly. Is this a scope problem? here is my code:
JS
var labor_count = 0;
var part_count = 0;
function incrementLabor(count) {
labor_count = parseInt(count);
console.log(labor_count);
}
HTML/PHP
public function getTableRow()
{
return '
<tr>
<td>
<input
type="text"
name="work_description[]"
value="'.$this->description.'">
</td>
<td>
<input
class="hours calculate"
type="text"
name="hours[]"
value="'.$this->hours.'"
onchange="calculate();"
size="2">
</td>
<td>
<input
class="rate calculate"
type="text"
name="rate[]"
value="'.$this->rate.'"
onchange="calculate();"
size="2">
</td>
<td>
<input
type="button"
value="Remove"
onclick="
(function () {
var parent = this.parentNode.parentNode; //get the row node
table.deleteRow(parent.rowIndex); //Delete the row index behind it.
labor_count--;
console.log(labor_count);
if (labor_count === 0) {
var header = document.getElementById("labor_header");
header.parentNode.removeChild(header);
})()
};" >
</td>
</tr>
';
}
Here is the working function:
function removeIt(element) {
var parent = element.parentNode.parentNode; //get the row node
var table = parent.parentNode;
table.deleteRow(parent.rowIndex); //Delete the row index behind it.
labor_count--;
console.log(labor_count);
if (labor_count === 0) {
var header = document.getElementById("labor_header");
header.parentNode.removeChild(header);
}
}