2

i am trying to create an if and else statement in JavaScript wherein if the string is empty, it will send out an alert box that all forms must be filled up. If not, the data grid in the webpage will increment with all the information entered on the form. But it seems that my code is not working properly. Below is my javascript code

function addData(){ 
  if(document.getElementById("txt_description").isEmpty || document.getElementById("txt_update").isEmpty){
    alert("Missing information!!! Please fill up all the blank items");	
  }else{
    var table = document.getElementById("myTable");
    var row = table.insertRow(-1);
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    var cell3 = row.insertCell(2);
    var cell4 = row.insertCell(3);
    var cell5 = row.insertCell(4);
    var cell6 = row.insertCell(5);
			
    cell1.innerHTML = document.getElementById("input_name").value;
    cell2.innerHTML = document.getElementById("input_blank2").value;
    cell3.innerHTML = document.getElementById("txt_description").value;
    cell4.innerHTML = document.getElementById("input_status").value;
    cell5.innerHTML = document.getElementById("input_pageable").value;
    cell6.innerHTML = document.getElementById("txt_update").value;
				
  }
}

4

1 Answer 1

1

Change your if statement to this:

if(document.getElementById("txt_description").value == ""|| document.getElementById("txt_update").value == "")

.isEmpty does not exist in javascript! If your input is empty, comparing it to an empty string will be sufficient!

For dealing with null values:

if(document.getElementById("txt_description").value == ""|| 
document.getElementById("txt_update").value == "" || 
document.getElementById("txt_description").value == null || 
document.getElementById("txt_update").value == null)

EDIT:

As mentioned in one of the comments, .value cannot be null. So you cold simply use the following if statement:

if(document.getElementById("txt_description").value == ""|| 
    document.getElementById("txt_update").value == "")
Sign up to request clarification or add additional context in comments.

5 Comments

This won't work, because document.getElementById will return either an element or null, but never an empty string "", so it will always be false. I think you meant document.getElementById().value?
Edited @RobertAKARobin
Thank you James and Robert! I added what Robert commented on this post with your code and it is perfectly working now. Thanks a lot guys!
value will never be null.
Yup i did! apparently there is this timer when I can click the check the "accepted" button. Thanks robert

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.