Search code examples
javascripthtmlobject

convert HTML DOM element to JavaScript Object?


I have a web page with the element:

<div id='myid'></div>

In chrome dev tools, when I type in the console

document.getElementById('myid')

returns a html element,

<div id='myid'></div>

is there an alternative method I can use to view it and it's properties as a JavaScript object instead rather than a html element?

convert html to javascript

I have seen this post, but all I want to do is get the element, not do any parsing and build something new and ideally in dev tools.


Solution

  • is there an alternative method I can use to view it and it's properties as a JavaScript object instead rather than a html element?

    all I want to do is get the element, not do any parsing and build something new and ideally in dev tools

    Yes if you are only interested in the dev tools (console), you can always use console.dir() method, it's made for this:

    console.dir(document.getElementById('myid'));
    

    console.dir() method:

    Displays an interactive list of the properties of the specified JavaScript object. The output is presented as a hierarchical listing with disclosure triangles that let you see the contents of child objects.