I am trying to generate a random password (this code’s running perfectly), which will be pasted to the value to the Password text box, but facing a problem while calling the javascript function.
Here is my JavaScript :
function password() {
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var string_length = 8;
var randomstring = '';
var table = document.getElementById('table'),
rows = table.getElementsByTagName('tr'),
i,j, cells, password;
for (var i=0; i<string_length; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum,rnum+1);
}
for (i=0,j = rows.length; i<j;++i)
{
cells = rows[i].getElementsById('td');
if(!cells.length)
{continue;
}
password = cells[j].innerText;
alert("hi "+password);
}
alert("Password is : "+randomstring);
document.f2.password.value = randomstring;
}
Here is my Table :
<table name="table" id="table" cellpadding="4" cellspacing="2" border="1" bgcolor="" >
<tr name="tr" id="tr">
<th><input type="checkbox" name="allCheck" onclick="selectallMe()"></th>
<th>Emp ID</th>
<th>Device</th>
<th>Feature Status</th>
<th>Policy</th>
<th>Password Management</th>
</tr>
<tr>
<% while(rs.next()){ %>
<td><input type="checkbox" name="chkName" onclick="selectall()"></td>
<td><input type="text" name="empId" value="<%= rs.getString(1)%> " disabled="disabled" maxlength="10"></td>
<td><input type="text" name="device" value="<%= rs.getString(2)%>" disabled="disabled" maxlength="10"></td>
<td><input type="text" name="features" value="<%= rs.getString(3)%>" disabled="disabled" maxlength="60"></td>
<td><input type="text" name="policyName" value="<%= rs.getString(4)%>" disabled="disabled" maxlength="10"></td>
<td id="td" name="td"><input type="text" name="password" id="password" value="" ><input type="button" name="Password" value="Password" onclick="password()"><input type="reset" name="Reset" value="Rseset"></td>
</tr>
<% }
%>
</table>
In my JS what is going wrong?? As I want to iterate through Password column, Password() will assign the value to the Password cells.
i would prefer to put this in a comment but i don’t have the reputation to yet.
first off is there a specific reason you want the cells to be text input types? Since you are just disabling them on creation.
also look into the rows[index].cells(); and table.rows(); methods in JS. these will give you an array of the cells and rows. from there you can iterate to where you want.
visit to learn more http://www.javascriptkit.com/domref/tableproperties.shtml
hope it helps