Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

305
Views
Checkboxes doesn`t get value of a textarea

I need to create 4 checkboxes, which will get textarea value and execute some functions from the module. However, checkboxes dont get the value, and that is my problem. Ive tried to put text.addEventListener to each function and to checkbox eventListeners as well, but it doesn`t work.

Hope, you could give me some kind of solution. Thanks in advance!

let text = document.querySelector('#text');

;(function() {

    let a = (text.value.split(' ').filter(elem => elem != ''));
    let b = a.join('').split('');

    function wordsNumber() {
        console.log(a);
        console.log('The number of words is ' + a.length + '!');
    }

    function symbolsNumber() {
        length = 0;
        for (let elem of a) {
            length += elem.length;
        }
        console.log('The number of symbols without backspace is ' + length + '!');
    }

    function symbolsNumberWithBackspace() {
        console.log('The number of symbols with backspace is ' + text.value.length + '!');
    }

    function counter() {
        let count = {};
        for (let elem of b) {
            if (count[elem] === undefined) {
                count[elem] = 1;
            }
            else {
                count[elem]++;
            }
        }
        for (let elem in count) {
            count[elem]=(Number(count[elem]/b.length)*100).toFixed(2) + '%';
        }
        console.log(count);
    }
    window.func = {wordsNumber, symbolsNumber, symbolsNumberWithBackspace, counter}
})();

let checkbox1 = document.querySelector('#check1');
let checkbox2 = document.querySelector('#check2');
let checkbox3 = document.querySelector('#check3');
let checkbox4 = document.querySelector('#check4');

checkbox1.addEventListener('click', function () {
    if (checkbox1.checked) {
        func.wordsNumber();
    }
})

checkbox2.addEventListener('click', function () {
    if (checkbox2.checked) {
        func.symbolsNumber();
    }
})

checkbox3.addEventListener('click', function () {
    if (checkbox3.checked) {
        func.symbolsNumberWithBackspace();
    }
})

checkbox4.addEventListener('click', function () {
    if (checkbox4.checked) {
        func.counter();
    }
})
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

A solution is to update variables a and b on text input, like this:

let text = document.querySelector('#text');

;(function() {

    let a, b;
    text.addEventListener('input', function () {
      a = (text.value.split(' ').filter(elem => elem != ''));
      b = a.join('').split('');
    });

    function wordsNumber() {
        console.log(a);
        console.log('The number of words is ' + a.length + '!');
    }

    function symbolsNumber() {
        length = 0;
        for (let elem of a) {
            length += elem.length;
        }
        console.log('The number of symbols without backspace is ' + length + '!');
    }

    function symbolsNumberWithBackspace() {
        console.log('The number of symbols with backspace is ' + text.value.length + '!');
    }

    function counter() {
        let count = {};
        for (let elem of b) {
            if (count[elem] === undefined) {
                count[elem] = 1;
            }
            else {
                count[elem]++;
            }
        }
        for (let elem in count) {
            count[elem]=(Number(count[elem]/b.length)*100).toFixed(2) + '%';
        }
        console.log(count);
    }
    window.func = {wordsNumber, symbolsNumber, symbolsNumberWithBackspace, counter}
})();

let checkbox1 = document.querySelector('#check1');
let checkbox2 = document.querySelector('#check2');
let checkbox3 = document.querySelector('#check3');
let checkbox4 = document.querySelector('#check4');

checkbox1.addEventListener('click', function () {
    if (checkbox1.checked) {
        func.wordsNumber();
    }
})

checkbox2.addEventListener('click', function () {
    if (checkbox2.checked) {
        func.symbolsNumber();
    }
})

checkbox3.addEventListener('click', function () {
    if (checkbox3.checked) {
        func.symbolsNumberWithBackspace();
    }
})

checkbox4.addEventListener('click', function () {
    if (checkbox4.checked) {
        func.counter();
    }
})
<textarea id="text"></textarea>
<input type="checkbox" id="check1">
<input type="checkbox" id="check2">
<input type="checkbox" id="check3">
<input type="checkbox" id="check4">

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!