How to use the return output to display it inside an element in the HTML

Hello there, I am creating a simple webpage for a Caesar Cipher. I wrote a Cipher class and I am trying to map a specific method to the button in the HTML. How do I use the return output and display it in a different element in the HTML?
class Cipher {
constructor(shift=0, plain_text="", encrypted_string="") {
this.alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
this.shift = shift;
this.plain_text = plain_text;
this.text_length = 0;
this.encrypted_string = encrypted_string;
}

describe() {
console.log("");
for (const [attribute, value] of Object.entries(this)) {
console.log(`${attribute}: ${value}`);
}
console.log("");
}

setShift(shiftValue) {
this.shift = shiftValue;
}

setPlainText(plainText) {
this.plain_text = plainText;
}

setTextLength() {
this.text_length = this.plain_text.length;
}

encrypt() {
for (let i = 0; i < this.text_length; i++) {
let char = this.plain_text[i].toUpperCase();
if (char === " ") {
this.encrypted_string += char;
} else {
let location = this.alphabet.indexOf(char);
let new_location = (location + this.shift) % 26;
this.encrypted_string += this.alphabet[new_location];
}
}
return this.encrypted_string;
}
}

const shift = document.getElementById("shiftInput")
const plaintext = document.getElementById("plainTextArea")
const encryptBtn = document.getElementById("encryptBtn")
const encryptedString = document.getElementById("encryptedArea")

const cipherObj01 = new Cipher();

function displayOutput() {
cipherObj01.setShift(shift.value);
cipherObj01.setPlainText(plaintext.value);
cipherObj01.setTextLength();

const encryption = cipherObj01.encrypt()

encryptedString.value = encryption;
}

encryptBtn.addEventListener('click', displayOutput());
class Cipher {
constructor(shift=0, plain_text="", encrypted_string="") {
this.alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
this.shift = shift;
this.plain_text = plain_text;
this.text_length = 0;
this.encrypted_string = encrypted_string;
}

describe() {
console.log("");
for (const [attribute, value] of Object.entries(this)) {
console.log(`${attribute}: ${value}`);
}
console.log("");
}

setShift(shiftValue) {
this.shift = shiftValue;
}

setPlainText(plainText) {
this.plain_text = plainText;
}

setTextLength() {
this.text_length = this.plain_text.length;
}

encrypt() {
for (let i = 0; i < this.text_length; i++) {
let char = this.plain_text[i].toUpperCase();
if (char === " ") {
this.encrypted_string += char;
} else {
let location = this.alphabet.indexOf(char);
let new_location = (location + this.shift) % 26;
this.encrypted_string += this.alphabet[new_location];
}
}
return this.encrypted_string;
}
}

const shift = document.getElementById("shiftInput")
const plaintext = document.getElementById("plainTextArea")
const encryptBtn = document.getElementById("encryptBtn")
const encryptedString = document.getElementById("encryptedArea")

const cipherObj01 = new Cipher();

function displayOutput() {
cipherObj01.setShift(shift.value);
cipherObj01.setPlainText(plaintext.value);
cipherObj01.setTextLength();

const encryption = cipherObj01.encrypt()

encryptedString.value = encryption;
}

encryptBtn.addEventListener('click', displayOutput());
'
1 Reply
jj_roby_1993
jj_roby_1993OP2y ago
It works! It was the parentheses in the EventListener.
Want results from more Discord servers?
Add your server