This may help.
You can compare the code and comments to your original code
Code:
<script type="text/javascript">
//replaced intTaskNumber argument with a button object (n)
function RemoveTask(n)
{
//set the intTaskNumber variable from the buttons expando intCount property
intTaskNumber= n.intCount;
strFieldName = "txtTask" + intTaskNumber;
strFieldName2 = "btnTask" + intTaskNumber;
var objHandler = document.getElementById("lstValues");
objHandler.removeChild(document.getElementById(strFieldName));
objHandler.removeChild(document.getElementById(strFieldName2));
alert("Delete Code Invoked");
}
</script>
<script type="text/javascript">
function formvalidation(frmValues)
{
intCount = document.frmTestData.txtTaskCount.value;
intCount = parseInt(intCount) + parseInt(1);
document.frmTestData.txtTaskCount.value = intCount
strFieldName = "txtTask" + intCount;
strFieldName2 = "btnTask" + intCount;
alert('Name: ['+strFieldName2+']');
strSubName = "RemoveTask(" + intCount + ");"
var element = document.createElement("input");
element.setAttribute("type", "Textbox");
element.setAttribute("value", document.frmValues.txtNewValue.value);
element.setAttribute("name", strFieldName);
//you need to add an id to the element for getElementById use in RemoveTask
element.setAttribute("id", strFieldName);
var foo = document.getElementById("lstValues");
//Append the element in page (in span).
foo.appendChild(element);
var button = document.createElement("button");
button.value = "Remove Task";
button.name = strFieldName2;
//you need to add a button id for getElementById use in RemoveTask
button.id = strFieldName2;
//assign the current value of intCount as an expando property when the button is created
button.intCount=intCount;
//To removes the issue of intCount always returning its last assigned value,
//call RemoveTask with the clicked button object.
button.onclick = function(){RemoveTask(this)};
var foo = document.getElementById("lstValues");
//Append the element in page (in span).
foo.appendChild(button);
document.frmValues.txtNewValue.value = "";
return false;
}