Home > database >  stuck in if statement in JavaScript?
stuck in if statement in JavaScript?

Time:07-05

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();
    }
})

CodePudding user response:

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();
    }
})

CodePudding user response:

You can stop the function by return when one of your conditions meets, and you should use .value.length instead of .textLength for finding the length of input field value.

// 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!");
    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
  }

  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();
});

CodePudding user response:

Use headingTxt.length not headingTxt.textLength

CodePudding user response:

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

CodePudding user response:

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

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

}
  • Related