ilyo
ilyo

Reputation: 36401

How to get HTML node object and not the HTML itself with JavaScript

When I do

var el = document.querySelector('.some-class');

I get the plain HTML.

How then do I get access to the Javascript node element?

I can see it when I do dir(el) in the console.

Upvotes: 0

Views: 1853

Answers (2)

Stephen Thomas
Stephen Thomas

Reputation: 14053

Actually, document.querySelector() returns an element object, which implements the Element interface which inherits from the Node interface.

Upvotes: 1

djalmaaraujo
djalmaaraujo

Reputation: 94

This is the dom itself. You can manipulate the element with only this. Why it is showing the HTML, it is just a common way that the console shows you. If you want to add a class for example, you can do.

var el = document.querySelector('.some-class');
el.classList.add('test')

This will work as it should. If you want to HTML of the element, you could also do:

el.outerHTML

Hope this helps you.

Upvotes: 1

Related Questions