aWhite89
05-30-2009, 06:02 PM
I am working on a simple control panel where I have text input fields in which a user can click on a text box and the text will automatically focus and select. I have done this fine. However, when I use the same onclick event for multiple fields, only the last one created works. Here is what I have:
var object = clearAndBuild();
var parent = object.x;
var box = object.y;
<?php
$ID = $_SESSION['userID'];
$query = mysql_query("SELECT `loginName`, `password`, `email`, `biography`, `pictureid` FROM `doctors` WHERE `ID` = '$ID'") or die("Could not run query");
$doctor = mysql_fetch_assoc($query);
?>
box.innerHTML += '<b>Username: </b>';
var userName = document.createElement('<input>');
userName.name = "userName";
userName.type = "text";
userName.id = "inputs";
userName.style.cursor = "pointer";
userName.size = "15";
userName.maxlength = "30";
userName.value = "<?php print $doctor['loginName']; ?>";
userName.onclick = function() { selectText(userName.id);};
box.appendChild(userName);
box.innerHTML += '<br><br><b>Password: </b>';
var password = document.createElement('<input>');
password.name = "password";
password.type = "password";
password.id = "pw";
password.style.cursor = "pointer";
password.size = "15";
password.maxlength = "30";
password.value = "<?php print $doctor['password']; ?>";
password.onclick = function() { selectText(password.id);};
password.onkeydown = function() {
var password2 = document.createElement('<input>');
password2.name = "password2";
password2.type = "password";
password2.id = "pw2";
password2.size = "15";
password2.maxlength = "30";
box.innerHTML += '<br><b>Confirm Password: </b>';
box.appendChild(password2);
document.getElementById("password").focus();
};
box.appendChild(password);
box.innerHTML += '<br><br><b>E-mail: </b>';
var email = document.createElement('<input>');
email.name = "email";
email.type = "text";
email.id = "email";
email.style.cursor = "pointer";
email.size = "15";
email.maxlength = "30";
email.value = "<?php print $doctor['email']; ?>";
email.onclick = function() { selectText(email.id);};
box.appendChild(email);
parent.appendChild(box);
function selectText(field)
{
document.getElementById(field).focus();
document.getElementById(field).select();
}
So, if I were to comment out the password and email field additions, then the userName field would work correctly. If I were to just comment out the email field, password would work correctly but userName would not. And finally, if I have the code as is, userName and password do not function correctly while email does.
Any suggestions for this are appreciated.
var object = clearAndBuild();
var parent = object.x;
var box = object.y;
<?php
$ID = $_SESSION['userID'];
$query = mysql_query("SELECT `loginName`, `password`, `email`, `biography`, `pictureid` FROM `doctors` WHERE `ID` = '$ID'") or die("Could not run query");
$doctor = mysql_fetch_assoc($query);
?>
box.innerHTML += '<b>Username: </b>';
var userName = document.createElement('<input>');
userName.name = "userName";
userName.type = "text";
userName.id = "inputs";
userName.style.cursor = "pointer";
userName.size = "15";
userName.maxlength = "30";
userName.value = "<?php print $doctor['loginName']; ?>";
userName.onclick = function() { selectText(userName.id);};
box.appendChild(userName);
box.innerHTML += '<br><br><b>Password: </b>';
var password = document.createElement('<input>');
password.name = "password";
password.type = "password";
password.id = "pw";
password.style.cursor = "pointer";
password.size = "15";
password.maxlength = "30";
password.value = "<?php print $doctor['password']; ?>";
password.onclick = function() { selectText(password.id);};
password.onkeydown = function() {
var password2 = document.createElement('<input>');
password2.name = "password2";
password2.type = "password";
password2.id = "pw2";
password2.size = "15";
password2.maxlength = "30";
box.innerHTML += '<br><b>Confirm Password: </b>';
box.appendChild(password2);
document.getElementById("password").focus();
};
box.appendChild(password);
box.innerHTML += '<br><br><b>E-mail: </b>';
var email = document.createElement('<input>');
email.name = "email";
email.type = "text";
email.id = "email";
email.style.cursor = "pointer";
email.size = "15";
email.maxlength = "30";
email.value = "<?php print $doctor['email']; ?>";
email.onclick = function() { selectText(email.id);};
box.appendChild(email);
parent.appendChild(box);
function selectText(field)
{
document.getElementById(field).focus();
document.getElementById(field).select();
}
So, if I were to comment out the password and email field additions, then the userName field would work correctly. If I were to just comment out the email field, password would work correctly but userName would not. And finally, if I have the code as is, userName and password do not function correctly while email does.
Any suggestions for this are appreciated.