开发者

checking whether an id exists with dojo

开发者 https://www.devze.com 2023-03-17 19:58 出处:网络
I\'m trying to check whether an html element with a certain id exists before doing some operations on that.

I'm trying to check whether an html element with a certain id exists before doing some operations on that.

How can I check whether an id exists or not with dojo?

I saw in javascript we can use try catch. But i like a more clean way.

edit:

Doing it like this:

 var a = dojo.b开发者_运维知识库yId('myId');
 if(a){
     // something
 }


In dojo, it's just the same as plain javascript. You should do:

var elem = dojo.byId('myId');
 if(elem != null){
     // something
 }

Hope this helps. Cheers


Use getElementById() - it returns null if no element matches, otherwise it returns a reference to the matching element. So:

var el = document.getElementById('someid');
if (el != null) {
  // element exists; do something, e.g.,
  alert(el.value);
}

(P.S. I don't know how to do it using dojo, but you don't need to...)

0

精彩评论

暂无评论...
验证码 换一张
取 消