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

125
Views
stuck in if statement in JavaScript?

I tried this but its not working

i want to be execute both the if statements if (addTxt.textLength == 0) And if (headingTxt.textLength == 0) and then, if both the condition will true then else block to be execute but its not working help me to get this issue resolve

    // If user adds a note, add it to the localStorage
let addBtn = document.getElementById('addBtn');
addBtn.addEventListener('click', function (e) {

    let addTxt = document.getElementById("addTxt");
    let headingTxt = document.getElementById("headingTxt");
    if (addTxt.textLength == 0) {
        alert("Please write something in text box!")

    }
    if (headingTxt.textLength == 0) {
        alert("Please Give some Heading To Your Note!")
    }
    else {
        let notes = localStorage.getItem("notes");
        if (notes == null) {
            notesObj = [];
        }
        else {
            notesObj = JSON.parse(notes);
        }
        notesObj.push({text: addTxt.value, heading: headingTxt.value, date : new Date()});
        localStorage.setItem("notes", JSON.stringify(notesObj));
        addTxt.value = "";
        // console.log(notesObj);
        showNotes();
    }
})
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I got the solution

// If user adds a note, add it to the localStorage
let addBtn = document.getElementById('addBtn');
addBtn.addEventListener('click', function (e) {

    let addTxt = document.getElementById("addTxt");
    let headingTxt = document.getElementById("headingTxt");
    if (addTxt.value.length == 0) {
        alert("Please write something in text box!")

    }
    else if (headingTxt.value.length == 0) {
        alert("Please Give some Heading To Your Note!")
    }
    else {
        let notes = localStorage.getItem("notes");
        if (notes == null) {
            notesObj = [];
        }
        else {
            notesObj = JSON.parse(notes);
        }
        notesObj.push({text: addTxt.value, heading: headingTxt.value, date : new Date()});
        localStorage.setItem("notes", JSON.stringify(notesObj));
        addTxt.value = "";
        // console.log(notesObj);
        showNotes();
    }
})
about 4 years ago · Juan Pablo Isaza Report

0

you can do something like this.

if (addTxt.value.length === 0 && headingTxt.value.length === 0) {
//do this
}
else {
    if (addTxt.value.length === 0) {
        alert("Please write something in text box!");
        return; //stop further logic in this function
    }
    if (headingTxt.value.length === 0) {
        alert("Please Give some Heading To Your Note!");
        return; //stop further logic in this function
    }
}
about 4 years ago · Juan Pablo Isaza Report

0

it's simple. You just group them in one condition:

if (addTxt.textLength == 0 && headingTxt.textLength == 0) {

}
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!