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?
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With