Click here to Skip to main content
15,665,942 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am having problems grasping event listener functions for a series of button inputs and text inputs linked to arrays. All the functions focus on a single task and are relatively simple-- at least in conception, if not my execution.

Please find below the html for reference and the js for patchwork. I appreciate any help or tips you can provide.

If you want to see it in (in)action, here's a link to the page


What I have tried:

<!DOCTYPE html>
<head lang="en">

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="test.03.css" media="screen">


<div class="container">

    <textarea class="textarea" placeholder="Type Here"></textarea>

    <!--Letter Labels-->

    <!--Blue Letter Selection-->
    <button class="lb">Blue</button>
    <button class="bA button" data-typed="A" data-regex=/[A]/ data-displayed="bA" data-selected="false">bA</button>
    <button class="bO button" data-typed="O" data-regex=/[O]/ data-displayed="bO" data-selected="false">bO</button>
    <button class="bC button" data-typed="C" data-regex=/[C]/ data-displayed="bC" data-selected="false">bC</button>
    <button class="bD button" data-typed="D" data-regex=/[D]/ data-displayed="bD" data-selected="false">bD</button>

    <!--Gold Letter Selection-->
    <button class="lg">Gold</button>
    <button class="gA button" data-typed="A" data-regex=/[A]/ data-displayed="gA" data-selected="false">gA</button>
    <button class="gO button" data-typed="O" data-regex=/[O]/ data-displayed="gO" data-selected="false">gO</button>
    <button class="gC button" data-typed="C" data-regex=/[C]/ data-displayed="gC" data-selected="false">gC</button>
    <button class="gD button" data-typed="D" data-regex=/[D]/ data-displayed="gD" data-selected="false">gD</button>

    <input class="concatenationarea">

<script src="test.03.js"></script>


<pre lang="Javascript">
let bttns = document.querySelectorAll(".button").forEach();
let keys = document.getElementById("textarea").forEach();
let nucarray = []
let concatarray = [] 

// Toggle button select on/off
function buttonToggle(bttns) {
    if ( == false) {'selected'); == true;
    else if ( == true) {'selected'); == false;

// Deselect prior selected buttons of the same letter
function buttonLetterCheck(element, bttns) {
    if ( ==
    { == false; }
    else { return }

// Deselect prior selected vowels
function buttonVowelCheck(element, bttns) {
    if ( - typed === "A" || - typed === "O")
        { - selected == false; }
    else if ( - typed === "O" || - typed === "A")
        { - selected == false; }
    else { return}

// Only allow button selected letters to be typed into the text area
function textRegex(keyPressed, bttns, event) {
    allowed_chars ==;
    if (!allowed_chars.test(event.key)) {
    else { return }

// Text Area Display: show array of color coded typed letters bracketed by start and end
function textDisplay(keypress) {
    if (Keypressed != {
        delete (keyPressed);
    else {
        textarea.color = bttns.background-color();
        nucarray.push( - displayed);

    textarea.value = '';


    textarea.value = nucarray.join;


//Concatenation Area Display: show daisy chain mini version of the button images bracketed by start and end buttons
function concatDisplay(keypress, bttns) {
    if (Keypressed != {
        delete (keyPressed);
    else {
        concatenationarea.value = '';


        concatenationarea.value = concatarray.join();

bttns.addEventListener("click", bttnToggle);
bttns.addEventListener("click", bttnLetterCheck);
bttns.addEventListener("click", bttnVowelCheck);
keys.addEventListener("keypress, click", textRegex);
keys.addEventListener("keypress", textDisplay);
keys.addEventListener("keypress", concatDisplay);


if (bttns.selected == true) {
Updated 29-Nov-21 8:33am

1 solution

There are a few semantic issues with your JavaScript that I see. For example, a getElementById would return a single element, then why use a forEach? If your HTML has multiple elements with the ID, then the HTML is invalid — but JavaScript will still return a single element.

Please see this: Document.getElementById() - Web APIs | MDN[^]

Secondly, the forEach method (focus on the word method) does not have a ".data-selected" field — unless, of course, you added on. That is why it would not work.

Please see this: Array.prototype.forEach() - JavaScript | MDN[^]

Also, in JavaScript, you can access the element properties using the index notation as well, like:
if (bttns["data-selected"] == false) {
The syntax that you are using would be considered as if you are trying to access the data field on bttns object and then subtracting the selected variable, and then ... You see where I am going?

There are a lot of areas that you need to improve there in the code, but since you asked the question about the event listener functions, I don't see that would be an issue. If I had to write this code, I would — apart from writing this code in a component-fashion — simply add a method that would handle everything for me.

Introduction to events - Learn web development | MDN[^]

Pass the object to the method and then operate on it. Instead of querying and then reading a property to mutate another property.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900