H
Henry Su
New to Javascript and have two questions, thanks in advance for any
pointers!!
1) Is there a way to change the property of an eventHandler, for
example -> <button id='btn123' onClick='abc()'> then after a user
input is dected ELSEWHERE, change the same button to behave
differently, eg. <button id='btn123' onClick='def()'>?
I tried this (a shot in the dark):
var button = document.getElementById('123');
button.onClick = 'def()';
2) The second question is slightly more complex - I have found a
snippet of code on the web that adds and removes rows to a table
dynamically, the key when removing a row is that the function realigns
all the row below the one removed so that the IDs will be consecutive
(convenient for the next stage processing).
However, when the html table is wrapped in a form, the Remove button
stops working - I GOT AROUND THE PROBLEM WITH ANCHORS - but the
question is still quite baffling - the error associated is that Object
doesn't support this property or method at line 1, char 1.
Attached please find the shortened version of the code - if the form
tags are taken out - then the code works fine.
<script type="text/JavaScript">
i=1;
function addRow(id){
var col1 = "<input type='text' id='email"+i+"' size='30'
maxlength='255'>";
var col3 = "<input type='button' name='remove' value='Remove'
onClick='remove(this.parentNode.parentNode.rowIndex)'>";
var tbody = document.getElementById(id).getElementsByTagName("TBODY"
)[0];
var row = document.createElement("TR" )
var td1 = document.createElement("TD" )
td1.appendChild(document.createElement(col1))
var td3 = document.createElement("TD" )
td3.appendChild (document.createElement(col3))
i=i+1;
row.appendChild(td1);
row.appendChild(td3);
tbody.appendChild(row);
}
function remove(rowIndex){
document.getElementById('myTable').deleteRow(rowIndex);
i = i-1;
var rows=document.getElementsByTagName("TR");
for (k = rowIndex; k < rows.length; k ++) {
inputs = rows[k].getElementsByTagName("input");
currentCol1 = inputs[0];
currentCol1.id = "email" + k;
}
}
</script>
<html>
<head>
<title>Document</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
</head>
<body>
<form>
<table id="myTable" cellspacing="0" border="1">
<tbody>
<tr>
<td>row1_column1</td><td align="center">row3_column3</td>
</tr>
</tbody>
</table>
<a href="javascript:addRow('myTable')">Add row</a>
</form>
</body>
</html>
THANKS AGAIN!!
pointers!!
1) Is there a way to change the property of an eventHandler, for
example -> <button id='btn123' onClick='abc()'> then after a user
input is dected ELSEWHERE, change the same button to behave
differently, eg. <button id='btn123' onClick='def()'>?
I tried this (a shot in the dark):
var button = document.getElementById('123');
button.onClick = 'def()';
2) The second question is slightly more complex - I have found a
snippet of code on the web that adds and removes rows to a table
dynamically, the key when removing a row is that the function realigns
all the row below the one removed so that the IDs will be consecutive
(convenient for the next stage processing).
However, when the html table is wrapped in a form, the Remove button
stops working - I GOT AROUND THE PROBLEM WITH ANCHORS - but the
question is still quite baffling - the error associated is that Object
doesn't support this property or method at line 1, char 1.
Attached please find the shortened version of the code - if the form
tags are taken out - then the code works fine.
<script type="text/JavaScript">
i=1;
function addRow(id){
var col1 = "<input type='text' id='email"+i+"' size='30'
maxlength='255'>";
var col3 = "<input type='button' name='remove' value='Remove'
onClick='remove(this.parentNode.parentNode.rowIndex)'>";
var tbody = document.getElementById(id).getElementsByTagName("TBODY"
)[0];
var row = document.createElement("TR" )
var td1 = document.createElement("TD" )
td1.appendChild(document.createElement(col1))
var td3 = document.createElement("TD" )
td3.appendChild (document.createElement(col3))
i=i+1;
row.appendChild(td1);
row.appendChild(td3);
tbody.appendChild(row);
}
function remove(rowIndex){
document.getElementById('myTable').deleteRow(rowIndex);
i = i-1;
var rows=document.getElementsByTagName("TR");
for (k = rowIndex; k < rows.length; k ++) {
inputs = rows[k].getElementsByTagName("input");
currentCol1 = inputs[0];
currentCol1.id = "email" + k;
}
}
</script>
<html>
<head>
<title>Document</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
</head>
<body>
<form>
<table id="myTable" cellspacing="0" border="1">
<tbody>
<tr>
<td>row1_column1</td><td align="center">row3_column3</td>
</tr>
</tbody>
</table>
<a href="javascript:addRow('myTable')">Add row</a>
</form>
</body>
</html>
THANKS AGAIN!!