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
2 Answers
Reset to default 7In 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...)