DEV Community

german9304
german9304

Posted on

3 1

javascript: DOM (document object model)

What is the DOM?

The DOM is a representation of the html elements in your document as a tree.

What does this mean?

It means that this html gets represented as a tree, so you can manipulate it with javascript.


<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>DOM</title>
  </head>
  <body>
    <div class="container">
      <p id="pragraph">my paragraph</p>
    </div>
    <section>
      <header>
        <h1>header</h1>
      </header>
      <div class="container">
        <p class="counter">0</p>
      </div>
    </section>
  </body>
</html>

How do I access the DOM through javascript?

Well there are a couple of ways, however you need the global property called document, here is an example:

// By using querySelector or getElementById or getElementsByClassName

// Query selector 
  const paragraph = document.querySelector('#pragraph');
  console.log(paragraph); // this will get the paragraph element. 

// Or get element By id 
const paragraphById = document.getElementById('pragraph');
console.log(paragraphById);

To access any element we can just call querySelector:

  const div = document.querySelector('div'); // first div
  const divs = document.querySelectorAll('div'); // all divs
/*
 One thing to notice here is that 
 document.querySelectorAll will get all the divs,
 and document.querySelector will get the first div it encounters. 
*/

Also by accessing the DOM with javascript you can manipulate it however you want, for example we can access the counter element and increment it by 1

const counter = document.querySelector('.counter');
counter.textContent = '1';

One import thing to say is that js must be inside the script tag or you can import a file

<!--- no file --->
 <script>
 //  here goes your js  
</script>

<!--- file --->
<script src="./somescriptfile"> </script> 

Thank you and please leave some feedback :) I hope you enjoy the reading.

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️