How to handle getElementById return Null

后端 未结 4 1303
感动是毒
感动是毒 2021-01-27 13:22

On our web application there are many pages. Some of them contain element \"Ribbon.ListForm.Display.Manage.Workflows-Medium\" while some pages not.

I would like to use s

4条回答
  •  伪装坚强ぢ
    2021-01-27 14:24

    Even if the element is not existing in the page, the return type will be object and return value will be null. so, you can check the null case also. please see the modified code.

    function hideEdit() {
        var edit = document.getElementById("Ribbon.ListForm.Display.Manage");
        if ( edit != null && typeof edit !== "undefined" && edit.value == ''){edit.style.display = "none";};
        var wf = document.getElementById("Ribbon.ListForm.Display.Manage.Workflows-Medium");
        if (wf != null && typeof wf !== "undefined" && wf.value == ''){wf.style.display = "none";}
        var checkout = document.getElementById("Ribbon.ListForm.Display.Manage.CheckOut-Large");
        if (checkout != null && typeof checkout !== "undefined" && checkout.value == ''){checkout.style.display = "none";}
    

    }

    thanks, varun.

提交回复
热议问题