最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - checking whether an id exists with dojo - Stack Overflow

programmeradmin4浏览0评论

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.byId('myId');
 if(a){
     // something
 }

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.byId('myId');
 if(a){
     // something
 }
Share Improve this question edited Jul 6, 2011 at 8:17 coder247 asked Jul 6, 2011 at 7:58 coder247coder247 2,94319 gold badges51 silver badges71 bronze badges 3
  • i dont get u right do u mean u need if element exist with given id ? – Marwan Commented Jul 6, 2011 at 8:04
  • what does "id exists" mean: a) an html element with a certain id b) a dijit with a certain id c) a variable with a certain name d) A dance of the voodoo god living behind the moon that categorizes his dances with ids – Steffen Commented Jul 6, 2011 at 8:05
  • 1 Simplify: if (dojo.byId('myId')) { ... } – Stephen Chung Commented Jul 6, 2011 at 9:47
Add a ment  | 

2 Answers 2

Reset to default 7

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)

  1. 暂无评论