9

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?

7
  • 3
    Why would you want that string? Just for debugging purposes? Would JSON.stringify come close? Commented Jul 28, 2013 at 4:23
  • I want to use it in my blog: the first line is the code, the second line is a button. After i click the button, the console string of the code appears below. Since there may be many jquery operations, it will be better than open the console, find the specific line , go back to the page and find its corresponding operation. Commented Jul 28, 2013 at 4:51
  • If you're doing JS development, that console should be permanently open, right? So I suppose I don't get the use case... Commented Jul 28, 2013 at 4:56
  • You are right. But i want every blog visitor will see the result, and i don't think everyone will open the console, and append result under the code is more visual. Commented Jul 28, 2013 at 5:00
  • 1
    @cameron Yeah there is no "standard" way to render an array of DOM elements succinctly as a string. Each browser likely will do that differently anyway. Commented Jul 28, 2013 at 5:08

3 Answers 3

3

You can't. console.log does some magic that a string cant do. It doesn't just render the object as a string. It also make the object structure interactive and clickable. It's rendering multiple interactive elements. The dev panel is taking a reference to an object and rendering it outside of where you code can reach.

But as @minitech notes, you can get close with JSON.stringify(myObject).

var myObject = { a: 1, b: 2 };
myObject.toString();      // [object Object]
JSON.stringify(myObject); // {"a":1,"b":2}

Though this won't work with DOM elements, as they typically have circular references. So you'd have to write your own function to crawl a jQuery object and dump it to a string. There isn't anything built in to do it for you.

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

1 Comment

I got it.Thanks. PS: i didn't vote against the answer. Someone else does.
3

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 :)

Comments

1

You could try util-inspect

This is an extraction of Node's inspect utility from the util module, with two fundamental advantages:

  • Single, focused module
  • Compatible with all browsers and environments.

Although it is compatible with browsers, it uses CommonJS so needs to be bundled first. But there's a fork that you can load directly in a browser via UNPKG.

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.