0

Related to my Question, I am facing trouble getting all elements I want to get all elements of a class using dojo.query my code is as below;

var jsonRoles = {"roles": [
                            {"roleId": "1", "roleName": "role1", "roleDesc": "This is role1"},
                            {"roleId": "2", "roleName": "role2", "roleDesc": "This is role2"},
                            {"roleId": "3", "roleName": "role3", "roleDesc": "This is role3"}
                        ]
                    };
                    var results="";
                    for(var i=0;i<jsonRoles.roles.length;i++){
                        results += '<div class="dojoDndItem ">' + '<span style="visibility: hidden">' + jsonRoles.roles[i].roleId + '</span>' + jsonRoles.roles[i].roleName  + '</div>';
                    }
                    var list = dojo.query(".dojoDndItem");

I want to store elements with class name dojoDndItem in variable "list" but when i inspect the "list" in firebug, it gives me "[]" (empty array). Am I doing something wrong?

Community
  • 1
  • 1
AbdulAziz
  • 5,868
  • 14
  • 56
  • 77

1 Answers1

0

dojo.query returns an array of matching DOM nodes from the page based on a CSS selector.

The variable results in your question is just a string with text like:

<div class="dojoDndItem"><span style="visibility: hidden">...

It's just a string. It's not an array of DOM elements, nor have you added any new elements to the page DOM itself, so unless there are pre-existing elements with the dojoDndItem class, empty array [] is the correct result.

Hamish
  • 22,860
  • 8
  • 53
  • 67
  • Can you explain by some suggested solution for this issue? – AbdulAziz May 01 '12 at 08:39
  • Solution *to what*? Did you want to add the HTML in `results` to the page? Did you want to create an array of DOM elements unattached to the page?` – Hamish May 01 '12 at 08:43