Forum Moderators: open

Message Too Old, No Replies

dojo.query question

         

deltaG

5:41 pm on Sep 5, 2008 (gmt 0)

10+ Year Member



I have been searching for an answer to my question but I haven't found it yet. I figured I would ask here to see if someone could clear something up.

I am using Dojo, and am just experimenting.
I am having a problem using the indexOf method on a dojo.query nodelist.

Basically I have 5 list items in an unordered list.

I created a node list of all the li elements. (treating it like an array)
>>> var elementlist = dojo.query("li");

This displays all the elements in the node list
>>> console.log(elementlist);
[li, li#current, li, li, li]

Here I create a variable named index to use the dojo array method of indexOf to search the array 'elementlist' for the first time it sees "li#current".
>>> var index = dojo.indexOf(elementlist, "li#current");

If a match is not found the index = -1. If a match is found it should give me an index of 1 in this case...but it always seems to return -1. Either I am wrong in concept or syntax?

>>> console.log(index);
-1

Can anyone assist?
Thanks in advance.

deltaG

3:48 am on Sep 7, 2008 (gmt 0)

10+ Year Member



I was messing around with this tonight with a buddy of mine who knows more than me and he was also a bit dumbfounded. It seems that when the nodelist is created the element in the node list that we were trying to access doesn't allow you to reference it like you would with a basic array dojo.indexOf. It looked like the element actually gets returned as some type of internal Dojo object.

I still would appreciate any input.
Thanks

Fotiman

3:55 pm on Sep 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Welcome to WebmasterWorld. Your elementlist contains an array of element objects. dojo.indexOf(elementlist, "li#current") will search the array for a String object matching "li#current". It will not match the string to an element object for obvious reasons. I think the indexOf method is only meant to work on String elements, but I'm not really familiar with Dojo so I don't know for sure.

deltaG

6:47 pm on Sep 7, 2008 (gmt 0)

10+ Year Member



Ok...thanks for the input. In my tinkering I had gotten that impression that it was an object as opposed to a string. All the documentation I had found mentioned the ability to use indexOf with the nodelist but I hadn't seen any examples of how that would be done.

So it makes sense that when I searched for the object reference that it was found whereas when searching for the string it was not. I guess all I would have to do is then search for the id or whatever property I wanted to find using the filter or do a forEach with a custom function doing what I want to.

Thanks