After implementing a code that generates different variables based on the 'truth' variable, I encountered an issue. Everything works flawlessly when 'truth' is set to "name," but as soon as I switch it to "email," any keystroke results in duplicate outputs. Any guidance on this matter would be highly appreciated.
var cmd = "";
var cmd1 = "";
var truth = "name";
$(document).ready(function() {
$("#contactlink a").click(function(){
$(document).keyup(function(event){
if (truth == "name"){
if (event.which == 8){ //backspace deletes last character of string
cmd = cmd.substring(0, cmd.length - 1);
}
else if (event.which == 13){
var name = cmd;
$("#emailcontact").typed({
strings:["<br/><br/>Email Address: "],
typeSpeed: 10,
showCursor: false
});
truth = "email";
}
else if ((event.which >= 65 && event.which <= 90) || event.which == 32){
if (cmd.length == 0){
cmd = cmd + String.fromCharCode(event.which);
}
else if (cmd.substring(cmd.length-1,cmd.length) == " "){
cmd = cmd + String.fromCharCode(event.which);
}
else{
cmd = cmd + String.fromCharCode(event.which).toLowerCase();
}
}
$('#namecontact').text(cmd);
}
else if (truth == "email"){
if (event.which == 8){ //backspace deletes last character of string
cmd1 = cmd1.substring(0, cmd1.length - 1);
}
else if (event.which == 13){
var name = cmd1;
$("#emailcontact").typed({
strings:["<br/><br/>Email Address: "],
typeSpeed: 10,
showCursor: false
});
var cmd2;
}
else if ((event.which >= 65 && event.which <= 90) || event.which == 32){
cmd1 = cmd1 + String.fromCharCode(event.which).toLowerCase();
}
$('#emailcontact').append(cmd1);
}
});
});
});