0

I have been trying to do an if else condition in the append method however for some reason it's not reading the condition. Here is the code. Towards the bottom I want to show signNow if the list item field is blank else show theForm (which is html that I assigned to a variable):

functionData();

function functionData(){
        var urlParam = GetUrlKeyValue("ID");       
        var method = "GetListItems";                 
        var webURL =  $().SPServices.SPGetCurrentSite();                     
        var list = "signature3";
        var fieldsToRead = "<ViewFields>"+"<FieldRef Name='ID' />" +"</ViewFields>";
        var query = "<Query><Where><Eq><FieldRef Name='ID'/><Value Type='Number'>" + urlParam + "</Value></Eq></Where></Query>";                       

        $().SPServices
        ({
            operation: method,
            async: false, 
            webURL: webURL,
            listName: list,
            CAMLViewFields: "<ViewFields Properties='True' />",
            CAMLQuery: query,                                                                                     
            completefunc: function (xData, Status)
            {
                $(xData.responseXML).SPFilterNode("z:row").each(function() 
                 {
                    var signNow = $(this).attr("ows_signature1");
                    var author = $(this).attr("ows_Author");
                    var created =  $(this).attr("ows_Created");
                    var author = author.slice(6);
                    var theForm = " <a id=\"create-user\" href=\"#\">Sign</a>"

                    $("#myHTMLTable").append("<tr>" +
                    "   <td width=\"50%\" style=\"font-size: 12pt;\">" +
                    "       Created:  " + created +
                    "   </td>" + 
                    "   <td width=\"50%\" style=\"font-size: 12pt;\">" +
                    "       By:  " + author +
                    "   </td>" +
                    "</tr>" +
                    "<tr>" +
                    "   <td width=\"50%\" class=\"tdHeader\">" +
                    "       Contracting Officer Representative (COR) Processing" +
                    "   </td>" +
                    "   <td width=\"50%\" class=\"tdSig right\">" +
                    //I want to "signNow" if the field is not blank else "theForm" if it's blank
                    signNow +
                    "</td>" +
                    "</tr>");
                });
            }
       });  
};

            </script>
1

Try replacing

signNow +

with

((signNow) ? signNow : theForm) +

You can read more about ternary operator here.

  • 1
    If I every meet you, I owe you a beer. Thanks! Works beautifully. I tried the ternary way but never worked but I was using operators. Again, many thanks! – adams-j Nov 7 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.