Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i have a few li's:

<ul>
    <li class="one">
        <input type="text" class="form_input" placeholder="Type something...">
    </li>
    <li class="two">
        <input type="text" class="form_input" placeholder="Type something else...">
    </li>
</ul>

i am trying to create a array similar to:

[
    0[
        'placeholder' : 'Type something...'
    ]
    1[
        'placeholder' : 'Type something else...'
    ]
]

i use a method like this

var lineText = function(elements) {
var myData = new Array();

elements.each(function(index, data) {
    var li = $(this);

    myData["placeholder"] = li.find('.form_input').attr("placeholder");

            console.log(data); returns the li objects
});

console.log(myData);
};

from this i get [placeholder: "Type something..."], basically one item in my array, instead of 2

i can't use push like this:

myData["placeholder"].push(li.find('.form_input').attr("placeholder")); //Cannot call method 'push' of undefined 

or add another dimension to the array

myData[index]["placeholder"] = li.find('.form_input').attr("placeholder"); //Cannot set property 'placeholder' of undefined 

any ideas? thanks

share|improve this question
    
Do you want objects as elements of an array? Or an array that contains strings? –  pimvdb Nov 8 '12 at 21:43
    
i don't want the objects, i want just the placeholder, in this case, or whatever i want to pass as a value. the objects are my data in this case –  Patrioticcow Nov 8 '12 at 21:44
    
You mean this? jsfiddle.net/vU36T –  pimvdb Nov 8 '12 at 21:45
    
an array can not have string keys like "placeholder" -- you might look into changing myData to an object instead. example var myData = {} instead of var myData = [] ( new Array() ) -- then add it like myData["placeholder"][index] = ... –  logic8 Nov 8 '12 at 21:48
1  
Do you need a multidimensional array? Are there more properties than 'placeholder' you want to add to the array? I'm assuming yes, in that case you want something more like this jsfiddle.net/vU36T/2 which uses objects. In JavaScript objects can act like associative arrays. –  SpaceFace Nov 8 '12 at 21:50
add comment

2 Answers

up vote 3 down vote accepted

It sounds like you want an array of "associative arrays". In JavaScript instead of associative arrays you can make objects can behave the same way. Meaning your above array would look like this in literal syntax.

var a = [ { 'placeholder': 'Type something...' },
          { 'placeholder': 'Type something else...' } ];

Where a[ 0 ][ 'placeholder' ] will return the string 'Type something...'. Since this uses objects, properties can't be pushed because they require some key. Instead you would write something like a.push( { 'placeholder': 'Type more stuff...' } ).

If you only plan to add placeholders to the array there's no need in using objects, just make an array of strings like this.

var placeholders = [ 'Type something...', 'Type something else...' ];

But when using my first solution, your function would become

var lineText = function(elements) {
    var myData = new Array();

    elements.each(function(index, data) {
        var li = $(this);
        var placeholderData = new Object();

        placeholderData["placeholder"] = li.find('.form_input').attr("placeholder");
        myData.push(placeholderData);
        console.log(data); returns the li objects
    });

    console.log(myData);
};

Live example here http://jsfiddle.net/vU36T/6/

share|improve this answer
    
thanks, i understand this better now –  Patrioticcow Nov 8 '12 at 22:33
add comment

this should work:

var placeholders = [];
$('ul > li > input').each( function(index, el){ placeholders.push(el.placeholder); } );

this will produce ["Type something...", "Type something else..."]

the reason you were getting errors is because you were trying to access an array with object syntax. Javascript arrays can only have numeric keys... However everything in javascript is an object which is why myData['placeholders'] shows up as 'undefined' and not a hard error. What it is doing is checking the Array prototype chain for a method or property 'placeholder', not the key 'placeholder' in the array myData.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.