Search code examples
javascriptdojo

Is there any way to use Dojo/query to grab classname with a specific number instead of getElementsByClassName


I wanted to change document.getElementsByClassName to dojo.query or query but wont' work:

var name =  document.getElementsByClassName("dijitReset dijitInputInner")[1];

this one works

var name  = dojo.query(".dijitReset dijitInputInner")[1];

this one won't work


Solution

  • .query returns an array-like NodeList, and getElementsByClassName returns an array-like HTMLCollection. The problem is not with that, but with your selectors.

    document.getElementsByClassName("dijitReset dijitInputInner")
    

    will select elements with either the class dijitReset or the class dijitInputInner.

    dojo.query(".dijitReset dijitInputInner")

    will select elements with a tag name of dijitInputInner that are descendants from an element with a class name containing dijitReset.

    You need to change it to:

    dojo.query(".dijitReset, .dijitInputInner")

    with the comma (to indicate a new selector) and a . (to indicate a search for class).