Manipulating DOM

Changing element’s id

mainHeading is reference object. We change the property of the
reference object and it will reflect back on the page.

You can also see the results by console

result:

getting the class names

result: heading subHeading

adding/removing a new class to an Element

subHeading.classList.add(‘special’);

You will then see the special class added to the element referenced by subHeading variable.

subHeading.classList.remove(‘special’);

Tag Name

console.log(‘mainHeading.tagName: ‘ + mainHeading.tagName); // H1

innerHTML

Contents of the elements
mainHeading.innerHTML = “” + mainHeading.innerHTML + ““;

outerHTML

Contents of the elements including the element itself.

mainHeading.outerHTML = “

” + mainHeading.innerHTML + “

“;

setAttribute

You can use methods setAttribute to

js

In the html you will see the attribute set in the h1 tag:

Does this element have attribute x ?