Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.byId('myId');
 if(a){
     // something
 }
like image 421
coder247 Avatar asked Jul 06 '11 07:07

coder247


2 Answers

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

like image 148
Edgar Villegas Alvarado Avatar answered Nov 06 '22 16:11

Edgar Villegas Alvarado


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...)

like image 39
nnnnnn Avatar answered Nov 06 '22 17:11

nnnnnn