How to find if element with specific id exists or not

前端 未结 6 1926
独厮守ぢ
独厮守ぢ 2020-12-08 04:18

In my JavaScript I want to check whether the element with specific id is exist or not, I tried it with 2 ways

1).

var myEle = document.getElementByI         


        
6条回答
  •  无人及你
    2020-12-08 05:04

     var myEle = document.getElementById("myElement");
        if(myEle){
            var myEleValue= myEle.value;
        }
    

    the return of getElementById is null if an element is not actually present inside the dom, so your if statement will fail, because null is considered a false value

提交回复
热议问题