У вас есть ошибка в элементе button
, это:
onClick= editarow()
должно быть
onclick='editarow()'
("C" должен быть в нижнем регистре, а вызов функции в кавычках.)
Давайте предположим, что строка выглядит следующим образом:
<tr>
<td data-foo="bar">John</td>
<td>Doe</td>
<td>...your edit button...</td>
</tr>
В editarow
вы можете сделать это:
function editarow() {
var row, firstNameCell, lastNameCell;
// `this` is the button, so `this.parentNode` is the cell, and
// `this.parentNode.parentNode` is the row
row = this.parentNode.parentNode;
// The first name cell is the first child
firstNameCell = findElement(row.firstChild);
// The second name cell is its next sibling
lastNameCell = findElement(firstNameCell.nextSibling);
// Their "values" are the text within them, most easily accessed via
// `innerHTML`
alert("First name is " + firstNameCell.innerHTML);
alert("Last name is " + lastNameCell.innerHTML);
// Or if you store data in attributes, you can get them via
// `getAttribute`
alert("data-foo = " + firstNameCell.getAttribute("data-foo"));
}
function findElement(node) {
while (node && node.nodeType != 1) {
node = node.nextSibling;
}
return node;
}
Все становится намного проще, если вы используете jQuery , Прототип , Закрытие или другие библиотеки . (Например, вышеприведенная функция findElement
очень грубая; ее цель - пропустить текстовые узлы и тому подобное.)