checking whether an id exists with dojo
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...)
精彩评论