There you have it: http://jsbin.com/odosoy/94/edit
Javascript
$(document).ready(function() {
$("#generate").click(function() {
getNewPassword();
});
function random(min, max) {
return min + parseInt(Math.random() * (max - min + 1), 10);
}
function generatePassword(charset) {
var length = parseInt($("#passlength").val(), 10);
var password = "";
while (length--) {
password += charset[random(0, charset.length - 1)];
}
return password;
}
function getNewPassword() {
var pass = generatePassword("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789!?#@*&.,;:+-=()[]_");
$('#password').val(pass);
}
});
HTML
Length: <input id="passlength"/><br>
Password: <input id="password"/><br>
<button class="btn btn-info" id="generate">Generate Password</button>
Update
Just reworked and styled your initial example a bit, nothing really special. http://jsbin.com/odosoy/97/edit and one in which the password is replaced: http://jsbin.com/odosoy/125/edit
Hope it helps.