erotavlas
erotavlas

Reputation: 4485

How to obtain a domNode by searching using classname instead of id?

I want to get a result exactly like dom.byId() returns, a domNode. However I cannot use id on my domNode. So my only other option is to search for it by class name instead of by id.

I tried

query(".classname").first()

because i know there is only one domNode that implements this class name

However I cannot use the result (which is a NodeList) in any subsequent functions in dojo that expect a domNode for example dojo/dom-geometry::position()

Upvotes: 0

Views: 62

Answers (1)

Vikash Pandey
Vikash Pandey

Reputation: 5443

Well, If you want to access the nodes using class name.

below is the working code -

require(["dojo/query", "dojo/NodeList-traverse", "dojo/NodeList-dom", "dojo/domReady!"], function(query) {
    query(".className").first().style({
        "backgroundColor": "#FF0"
    });
});
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"
            data-dojo-config="async: true"></script>

 <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/dojo/1.10.4/dijit/themes/claro/claro.css">

<ul>
    <li class="className">First</li>
    <li class="className">Second</li>
    <li class="className">Third</li>
</ul>

JS Fiddle : https://jsfiddle.net/vikash2402/jfwsLnd4/

Feel free to shoot your further queries.

Hoping this will help you :)

Upvotes: 1

Related Questions