Asim Zaidi
Asim Zaidi

Reputation: 28354

javascript array is empty

I have the following code. I am not sure why the myData array is empty in console.log as inputValue and inputName are not null and I can see them in log

var myData = new Array();

        $("#myelement :input").each(function(){

             var inputValue = $(this).val();             
            var inputName = $(this).attr('id');
             console.log(inputValue, inputName);
             myData.push=inputName;
             myData.push=inputValue;
             console.log(myData);
        });

Upvotes: 1

Views: 325

Answers (1)

Alex Wayne
Alex Wayne

Reputation: 187312

This isn't right:

myData.push=inputName;
myData.push=inputValue

Basically, you are setting the push property of the array to a value. But the push property of an array is already set to something, the function that adds an item to the array.

You want to call the push method instead, not replace it:

myData.push(inputName);
myData.push(inputValue);

Upvotes: 9

Related Questions