1
saveData: function()
    {

        var element = $('input');
        for(var i=0;i<element.length;i++)
        {
            //alert($(element[i]).val());
            var p=new Array($(element[i]).val());
        }
        alert(p);

    },

How to print array data in alert.

6
  • what is your problem? Commented Nov 22, 2013 at 6:20
  • It should work as it is. Commented Nov 22, 2013 at 6:22
  • possible duplicate of Print content of JavaScript object? Commented Nov 22, 2013 at 6:23
  • i want to show all for loop data in alert but it is not showing all for loop data in alert.Please help me. Commented Nov 22, 2013 at 6:23
  • If you are using firefox browser, make use of console.log() Commented Nov 22, 2013 at 6:24

6 Answers 6

5

You need to create an array and then push all the values to it, instead you are resetting it in the loop

var element = $('input');
var p = element.map(function () {
    return this.value
}).get();
alert(JSON.stringify(p));//or alert(p);

changing your code will be

var element = $('input');
var p = [];
for (var i = 0; i < element.length; i++) {
    p.push($(element).eq(i).val());
}
alert(JSON.stringify(p));//or alert(p)
Sign up to request clarification or add additional context in comments.

Comments

5

Use javascript toString() function to get result

var array = ["a","b","c"];

solution:

alert(array.toString());

Comments

0

In JavaScript you could just

for(i=0; i<p.length; ++i){
    alert(p[i]);
}

so if p is an array containing ["one","two","three"] your browser will alert "one", "two" and then "three" in the loop.

Comments

0

To display array values properly try this :

saveData: function()
    {
        var p=new Array();
        var element = $('input');
        for(var i=0;i<element.length;i++)
        {
            //alert($(element[i]).val());
            p[0] = $(element[i]).val();
        }
        alert(p.join("\n"));

    },

Comments

0

Use javascript forEach() function to get result

var array = ["a","b","c"]; 

solution:

array.forEach(function(data){alert(data)});

Comments

0

You can use join().

It converts each array element into a string. It acts much like toString(), moreover you can determine the separator.

var arr = ["HTML","CSS","JS"]; 
...
alert(arr.join(' - '));

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.