0

I am new to JQuery. I am here to learn JQuery fundamentals and serious to know how array works in jQuery.

<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
    <style>
  div { color:blue; }
  span { color:red; }
</style>
    <script src="jquery-latest.js"></script>
</head>
<body>

<div id="myDiv"></div>

<script>
$(document).ready(function() {

    var testArray = ["test1","test2","test3","test4"];
    var new_array=[];
    var vPool="";

    /*i tried but this pushing code dosen't work*/

   //pushing values in new_array
    $("new_array").push({"test1","test2","test3","test4"});

    //fetching the values of new_array 
    $.each(new_array, function(i, val) {
      vPool+=val + "<br />";
    });
    $('#myDiv').html(vPool);--->
    //vPool="";

    /*But i tried this code it work fine */

    //fetching the values of new_array 
    $.each(testArray, function(i, val) {
      vPool+=val + "<br />";
    });
    $('#myDiv').html(vPool); 
    //vPool="";

});
</script>

</body>
</html>

And I want to know how to print structured array like we print in PHP:

echo "<pre>";
print_r($array);
3
  • Are you trying to print in order to debug ? If so use console.log(arr) and look at the console. Commented Jan 8, 2013 at 8:20
  • Array is part of JavaScript. jQuery is a library written in JavaScript. Commented Jan 8, 2013 at 8:21
  • i tried console.log but it's doesn't work for me.may i don't aware how to use it.... Commented Jan 8, 2013 at 8:22

1 Answer 1

1

Remove the $("") arround new_array. Also you are pushing an object that is defined incorrect. And the push() function has nothing to do with jQuery, it is pure JavaScript.

About the printing of the whole array you should try console.log() in Chrome or Firefox with installed Firebug extension.

$(document).ready(function() {

    var testArray = ["test1","test2","test3","test4"];
    var new_array=[];
    var vPool="";

    /*i tried but this pushing code dosen't work*/

    //pushing values in new_array
    new_array.push("test1","test2","test3","test4");
    console.log(new_array);

    $.each(new_array, function(i, val) {
        vPool+=val + "<br />";
        console.log(val);
    });

});

The push() function returns the new length of the array.

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

7 Comments

:-$(new_array).push({test1,test2,test3,test4}); like this way
:-i don't know but doesn't work for me sir.i understand but how i know without print anything.....
console.log(new_array); Try this in chrome.
i understand but how i know values is pushing into array.i will try console.log(new_array);it's shows [] in firebug.any one tried this code and getting values.iam new to jquery please help me out.
The push() function returns the new length of the array.
|

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.