Checking if id exists with dojo

I am trying to check if an html element exists with a specific identifier before performing some operations on it.

How to check if an id exists with dojo?

I saw in javascript we can use try catch. But I like a cleaner way.

edit:

Doing this:

var a = dojo.byId('myId'); if(a){ // something } 
+7
source share
2 answers

In dojo, it looks like regular javascript. You must do:

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

Hope this helps. Greetings

+6
source

Use getElementById() - it returns null if no elements match, otherwise it returns a link to the corresponding element. So:

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

(PS I don't know how to do this using dojo, but you don't need ...)

+6
source

All Articles