1

I have various elements with same class (cCameras). I am able to push the elements into the empty array but for some reason I cannot access the elements inside the array.

var eCamerasArray = [];

// elements in this class are being pushed into the array 'cCameras'
$(".cCameras").each (function (){ eCamerasArray.push (this); }); 

// Firebug gives me an array with elements inside.
console.log (eCamerasArray);

for( var i = 0; i < eCamerasArray.length; i++) {    
   // Firebug gives me [undefined].
   console.log (eCamerasArray[i]);
}
6
  • 3
    Why do you want to do this? Just doing $(".cCameras") gets you a jQuery wrapped array of DOM elements, which you can access through the get method. No need to build up the array yourself. Commented Jan 17, 2012 at 18:24
  • Works fine jsfiddle.net/EygTk Commented Jan 17, 2012 at 18:26
  • Using your code seems to work in Chrome and Firefox for me. jsfiddle.net/47yMd. Are the elements defined? Commented Jan 17, 2012 at 18:27
  • Thank you guys, toArray() did it. I need the array for looping & modifying purposes. Cheers Commented Jan 17, 2012 at 18:31
  • Not working for me, I did copy & paste here :! Commented Jan 17, 2012 at 18:31

1 Answer 1

3

You could just write $(".cCameras").toArray() to get the same result, possibly one that works.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.