printing values of object in jquery

I have below code that get values from a grid. Instead of printing the values, the grid is printing data in below object format. How to print values?

[object Object], [object Object], [object Object], [object Object], [object Object]

CODE:

$(document).ready(function () {
    $("#check").click(function(){
        var rows = $('#jqxgrid').jqxGrid('getrows');
        alert(rows);
    });
});        

A simple way is to use

JSON.stringify(rows)

i.e.

alert(JSON.stringify(rows))

Else, you would need to manually traverse the object’s properties and print them accordingly. Give more details about input and desired output for an example.

An example in interactive Node.js:

> x = { 1: 2, 3:4 };
{ '1': 2, '3': 4 }

> x.toString();
'[object Object]'

> JSON.stringify(x)
'{"1":2,"3":4}'

In order to print value of objects in JQuery you can create a new array with values and print it:

var array =  $.map(object, function(value){
    return value;
})

Grid’s getrows returns an array of rows. To get the first row: var row = rows[0]. To get the second row use: var row2 = rows[1] and so on.

To get the ‘fname’ cell value from the first row, you can do this:

var rows = $("#grid").jqxGrid('getrows');
var firstRow = rows[0];
var fnameValue = firstRow.fname;

Got help from stackoverflow, Include following jquery function:

   The result will be equivalent to the PHP function print_r.
   echo '<pre>' . print_r($data) . '</pre>';

   USAGE: 
   var data = [{'id':1,'name':'hello'},'world'];
   $('#element').print_r(data);

//==========================================
(function($) {

    $.fn.print_r = $.fn.print = function(variable){
        return this.each(function(){
        if(typeof variable == 'object'){
            var string = $.print_r.objectToString(variable,0);
            $(this).html(string);
        } else {
            $(this).html('<pre>'+variable.toString()+'</pre>');
        }
    });

    }

    $.print_r = {
            objectToString : function (variable,i){
              var string = '';
              if(typeof variable == 'object' && i < 3){ // 3 is to prevent endless recursion, set higher for more depth
                  string += 'Object ( <ul style="list-style:none;">';
                  var key;
                  for(key in variable) {
                      if (variable.hasOwnProperty(key)) {
                        string += '<li>['+key+'] => ';
                        string += $.print_r.objectToString(variable[key],i+1);
                        string += '</li>';
                      }
                  }
                  string += '</ul> )';
              } else {
                  string = variable.toString();
              }
              return string;
        }
    }

})(jQuery)


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .
Read More:   reprompt for permissions with getUserMedia() after initial denial

Similar Posts