Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get result string of console.log in javascript code?

Tags:

javascript

For example, if I enter $("p") in the chrome console, I get [<p>Text1</p>,<p>..</p>] , which is just what I want. Now I want to store the result string to a variable and reuse it later, but i couldn't find a way yet ( $("p").toString() gets [Object Object]).

Is there a way to get the result string of console.log in code?

like image 236
cameron Avatar asked Jul 28 '13 04:07

cameron


1 Answers

Well, here’s something-ish:

function repr(obj) {
    if(obj == null || typeof obj === 'string' || typeof obj === 'number') return String(obj);
    if(obj.length) return '[' + Array.prototype.map.call(obj, repr).join(', ') + ']';
    if(obj instanceof HTMLElement) return '<' + obj.nodeName.toLowerCase() + '>';
    if(obj instanceof Text) return '"' + obj.nodeValue + '"';
    if(obj.toString) return obj.toString();

    return String(obj);
}

It isn’t interactive or comprehensive. If you need that, I can add it, but at a certain point it might just be easier to look at the WebKit developer tools’ source :)

like image 125
Ry- Avatar answered Nov 15 '22 00:11

Ry-