idkwtcms
1 year ago

Replacing an empty value with text in JavaScript

Posted 1 year ago by idkwtcms

So I have code that works well and pulls all the information as needed and such, I'm just trying to make it so that if a certain value comes back as empty that I can just replace the field with text that just says "n/a"

Here is my javascript code:

<script>
$(function() { 
    $('#policy-documents-search-form').on('submit', function(e) { 
        e.preventDefault();  
        $('#validationerror').empty();
        $("#form-group").empty();

        $.ajax({
            type: "POST",
            url: "",
            data: $('#policy-documents-search-form').serialize(), 
            dataType: 'JSON',
            success: function(response) {
                
                if(response.status == 'ok'){
                    $("#form-group").empty().append("<table><thead><tr><th>Name</th><th>Claims Telephone</th><th>Policy Wording</th><th>IPID</th></tr></thead><tbody><td>" + response.policyinfo[0] + "</td> <td>" +
                        response.policyinfo[1] + "</td> <td><a href=" + response.policyinfo[2] + ">Policy Wording </a></td><td><a href=" + response.policyinfo[3] + ">IPID </a></td></tr></tbody></table>");
                }

                if(response.status == 'fail'){
                    $('#validationerror').text(response.error);
                }
            },
            error: function(response) {

                $('#validationerror').text(response.responseJSON.ordernumber);
                console.log($('#validationerror').text());
            }
        });
      });
   });
  </script>

Please sign in or create an account to participate in this conversation.