Hah! Fancy that, I've still got the copy I used to answer a similar question last week or the one before. Basically, you use the addEventListener function.
When you do this, the function becomes a part of the element's javascript object. The consequence of this is that the 'this' keyword, when used in the called function is already pointing to the element that triggered the event. In this case, the button. This is why my code has no input variables for the onAddRowBtnClicked or the onDeleteRowBtnClicked functions - the function is attached to the button itself and you can navigate the DOM tree from there.
There are many other advantages of using
addEventListener
, rather than inline in the Html or with
element.onclick =
(note: the event for '.onclick' is actually called 'click' - the 'on' is removed. You only need the 'on' when specifying the event inline in the html or when _not_ using addEventListener.)
Hope you don't mind, I've pasted my code here, rather than changing yours.
<!DOCTYPE html>
<html>
<head>
<script>
function byId(e){return document.getElementById(e);}
function newEl(tag){return document.createElement(tag);}
function newTxt(txt){return document.createTextNode(txt);}
window.addEventListener('load', mInit, false);
// function taken from "The JavaScript PocketGuide" (Lenny Burdette), pg 147
function insertAfter(newNode, reference)
{
// If the reference node has a nextSibling, insert
// the node before that
if (reference.nextSibling)
{
reference.parentNode.insertBefore(newNode,reference.nextSibling);
// If the reference node is the last child, just
// append the node to the parent
}
else
{
reference.parentNode.appendChild(newNode);
}
return newNode;
}
var numAdded = 0;
function onAddRowBtnClicked()
{
var row = newEl('tr');
var td1 = newEl('td');
td1.appendChild( newTxt( 'dynamically added(' + numAdded + ')' ) );
numAdded++;
var td2 = newEl('td');
var addBtn = newEl('button');
addBtn.addEventListener('click', onAddRowBtnClicked, false);
addBtn.appendChild( newTxt('add a new row below this one') );
td2.appendChild(addBtn);
var td3 = newEl('td');
var delBtn = newEl('button');
delBtn.addEventListener('click', onDeleteRowBtnClicked, false);
delBtn.appendChild( newTxt('delete this row') );
td3.appendChild(delBtn);
row.appendChild(td1);
row.appendChild(td2);
row.appendChild(td3);
var clickedBtn = this;
var containingCell = clickedBtn.parentNode;
var containingRow = containingCell.parentNode;
//var owningTable = containingRow.parentNode;
insertAfter(row, containingRow);
//owningTable.appendChild(containingRow);
}
function onDeleteRowBtnClicked()
{
var clickedDelBtn = this;
var containingCell = clickedDelBtn.parentNode;
var containingRow = containingCell.parentNode;
var owningTable = containingRow.parentNode;
owningTable.removeChild(containingRow);
}
function mInit()
{
var delBtnList = document.querySelectorAll('button[name=delBtn]');
var onlyDelBtn = delBtnList[0];
onlyDelBtn.addEventListener('click', onDeleteRowBtnClicked, false);
var addBtnList = document.querySelectorAll('button[name=addBtn]');
var onlyAddBtn = addBtnList[0];
onlyAddBtn.addEventListener('click', onAddRowBtnClicked, false);
}
</script>
<style>
</style>
</head>
<body>
<table id='dynTable'>
<tr>
<td>Some Text</td><td><button name='addBtn'>Add row below this one</button></td><td><button name='delBtn'>delete this row</button></td>
</tr>
</table>
</body>
</html>