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

116
Views
Issue with textarea

I have a simple textarea. I want to check this area for text, when you push button and if your textarea is empty it fills it with default email.

const btn = document.getElementById('btn');
let x = document.getElementById('smth');

function putDefaultMail() {
  if (x.textContent != null) {
    x.textContent = 'defaultmail@gmail.com';
  }
}

btn.addEventListener('click', function(e) {
  e.preventDefault();
  putDefaultMail();
});
<form>
  <textarea id="smth"></textarea>
  <button id="btn">Sumbit</button>
</form>

But something went wrong. It just calls once.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

textContent is not the proper way of getting values from textarea. And also, a blank textarea's value is "", not null.

const btn = document.getElementById('btn');
let x = document.getElementById('smth');

function putDefaultMail() {
    if (x.textContent != null) {
        x.textContent = 'defaultmail@gmail.com';
    }
}

btn.addEventListener('click', function (e) {
    e.preventDefault();
    putDefaultMail();
});
<form>
                    <textarea id="smth"></textarea>
                    <button id="btn">Sumbit</button>
</form>

Instead, try .value:

const btn = document.getElementById('btn');
let x = document.getElementById('smth');

function putDefaultMail() {
    if (x.value == '') {
        x.value = 'defaultmail@gmail.com';
    }
}

btn.addEventListener('click', function (e) {
    e.preventDefault();
    putDefaultMail();
});
<form>
                    <label>Text: <textarea id="smth"></textarea></label>
                    <button id="btn">Sumbit</button>
</form>

By the way, you should add labels to the controls like above.

However, if the user enters spaces, then it passes. Like @MuhammadAliMalekzadeh said below, trim() should remove the spaces around for you.

const btn = document.getElementById('btn');
let x = document.getElementById('smth');

function putDefaultMail() {
    if (x.value
      .trim() // <-- here
    == '') {
        x.value = 'defaultmail@gmail.com';
    }
}

btn.addEventListener('click', function (e) {
    e.preventDefault();
    putDefaultMail();
});
<form>
                    <label>Write here: <textarea id="smth"></textarea></label>
                    <button id="btn">Sumbit</button>
</form>

about 4 years ago · Juan Pablo Isaza Report

0

Textarea has a value. You should check if value empty or not.

const btn = document.getElementById('btn');
let x = document.getElementById('smth');

function putDefaultMail() {
    if (x.value == "") { // if empty 
        x.value = 'defaultmail@gmail.com';
    }
}


btn.addEventListener('click', function (e) {
    e.preventDefault();
    putDefaultMail();
});
about 4 years ago · Juan Pablo Isaza 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!