var lotsData = [
{
index: 0,
data: 'I want to be in HTML',
},
{
index: 1,
data: 'I dont' want to be in HTML',
}]
Lets say I prefer to have one var with an array. How do I access the index 0, say its connected to a click event and I want to use data:
$('.tab').live('click', function() {
console.log("I was clicked");
$('#fancy').text(WHAT GOES HERE TO ACCESS data from index 0?);
});
jQuery is written in Javascript, and Javascript itself provides the Array object.
So accessing the 0th element of an array is
array_name[0]In your example, you’re storing objects as the elements of the array. Your objects are including an “index” property, but beware, your “index” property has nothing to do with the element index in the array. You should NOT include an “index” property… Eg:
The better example would be:
Added: trailing commas
Note that while Javascript is a powerful language, it does have its quirks.
An important one is trailing commas, such as
The problem is that the trailing comma is not a formal part of the Javascript language. Most JS engines support it, but a very important one does not: the Internet Explorer browser does not support trailing commas and will die a sad death when it encounters one.
Your testing should always include IE due to its unique ways of doing things.
I test in IE 7.