top
upGrad KnowledgeHut SkillFest Sale!

Search

JavaScript Tutorial

JavaScript can access every part of the HTML DOM(Document Object Model) and change the web-page or add event listeners.The HTML DOM is a standard for how to get, change, add, or delete HTML elements.DOM Methods in JavascriptThe HTML DOM can be accessed by the JavaScript language. We have properties and methods on DOM.Let’s take a look at the dom example. Here, we have both property and method.<!DOCTYPE html> <html>  <body>    <h1 id="demo"></h1>    <script>      document.getElementById('demo').innerHTML = 'Hello JavaScript!';    </script>  </body> </html>getElementById method in JavascriptThis method getElementById is used to get the object containing the HTML element with the given id. In the above example, we are calling document.getElementById('demo'), so it will return  <h1 id="demo"></h1>We can verify the same from the console for the web page, we created above.innerHTML propertyThis method innerHTML is used for getting or replacing the content of HTML elements. In the above example, we change the content of the <h1> tag. You can think of it changing the things inside the <h1> tag, which in the above example was empty.DOM Elements in JavascriptWe need to access HTML elements to work on it. We have already seen many examples of getElementById but there are some more also.Element By IdWe have already seen an example in the previous topic of DOM Methods for getting element by Id using getElementById  Elements By Tag NameThe method getElementsByTagName selects all tags of a given type in the web-page and return an array like object called HTMLCollection. In the below example we are getting the HTMLCollection in “var x”. Since there are two <p> tags in the page we can access them by x[0] and x[1].<!DOCTYPE html> <html>  <body>    <p>Hello JS!</p>    <p>This example is for you all JS People.</p>    <div id="demo"></div>    <script>      var x = document.getElementsByTagName('p');      document.getElementById('demo').innerHTML =        x[0].innerHTML + ' ' + x[1].innerHTML;    </script>  </body> </html>Output with console<!DOCTYPE html> <html>  <body>    <p class="intro">Hello JS!</p>    <p class="intro">This example is for you all JS People.</p>    <div id="demo"></div>    <script>      var x = document.getElementsByClassName('intro');      document.getElementById('demo').innerHTML =        x[0].innerHTML + ' ' + x[1].innerHTML;    </script>  </body> </html>Element By CSS SelectorWe can use the more modern querySelector method to match a specified CSS Selector(id, class).  The only difference from getElementById is that we need to add # for id or for class as we do in CSS.<!DOCTYPE html> <html>  <body>    <h1 id="demo"></h1>    <script>      document.querySelector('#demo').innerHTML = 'Hello JavaScript!';    </script>  </body> </html>Elements By CSS SelectorsWe can use the querySelectorAll method to find HTML elements which match a specified CSS Selector(id, class).  It returns an array like object called as NodeList. It can be used in place of getElementsByClassName  or getElementsByTagName.<!DOCTYPE html> <html>  <body>    <p>Hello JS!</p>    <p>This example is for you all JS People.</p>    <div id="demo"></div>    <script>      var x = document.querySelectorAll('p');      document.getElementById('demo').innerHTML =        x[0].innerHTML + ' ' + x[1].innerHTML;    </script>  </body> </html>Output with consoleDOM CSS in JavascriptWe can also change the CSS style of an HTML element. Below is an example of the same. Notice, that for CSS property like font-size, we have to use camel case like fontSize.<!DOCTYPE html> <html>  <body>    <div id="title">Hello JS!</div>    <p id="para">This example is for you all JS People.</p>    <script>      document.querySelector("#title").style.fontSize = "22px";      document.querySelector("#para").style.color = "blue";    </script>  </body> </html>Output
logo

JavaScript Tutorial

HTML DOM in Javascript

JavaScript can access every part of the HTML DOM(Document Object Model) and change the web-page or add event listeners.

The HTML DOM is a standard for how to get, change, add, or delete HTML elements.

DOM Methods in Javascript

The HTML DOM can be accessed by the JavaScript language. We have properties and methods on DOM.

Let’s take a look at the dom example. Here, we have both property and method.

<!DOCTYPE html>
<html>
 <body>
   <h1 id="demo"></h1>
   <script>
     document.getElementById('demo').innerHTML = 'Hello JavaScript!';
   </script>
 </body>
</html>

getElementById method in Javascript

This method getElementById is used to get the object containing the HTML element with the given id. In the above example, we are calling document.getElementById('demo'), so it will return  <h1 id="demo"></h1>

We can verify the same from the console for the web page, we created above.

JavaScript code

innerHTML property

This method innerHTML is used for getting or replacing the content of HTML elements. In the above example, we change the content of the <h1> tag. You can think of it changing the things inside the <h1> tag, which in the above example was empty.

DOM Elements in Javascript

We need to access HTML elements to work on it. We have already seen many examples of getElementById but there are some more also.

Element By Id

We have already seen an example in the previous topic of DOM Methods for getting element by Id using getElementById  

Elements By Tag Name

The method getElementsByTagName selects all tags of a given type in the web-page and return an array like object called HTMLCollection. In the below example we are getting the HTMLCollection in “var x”. Since there are two <p> tags in the page we can access them by x[0] and x[1].

<!DOCTYPE html>
<html>
 <body>
   <p>Hello JS!</p>
   <p>This example is for you all JS People.</p>
   <div id="demo"></div>
   <script>
     var x = document.getElementsByTagName('p');
     document.getElementById('demo').innerHTML =
       x[0].innerHTML + ' ' + x[1].innerHTML;
   </script>
 </body>
</html>

Output with console

JavaScript Code

<!DOCTYPE html>
<html>
 <body>
   <p class="intro">Hello JS!</p>
   <p class="intro">This example is for you all JS People.</p>
   <div id="demo"></div>
   <script>
     var x = document.getElementsByClassName('intro');
     document.getElementById('demo').innerHTML =
       x[0].innerHTML + ' ' + x[1].innerHTML;
   </script>
 </body>
</html>

Element By CSS Selector

We can use the more modern querySelector method to match a specified CSS Selector(id, class).  The only difference from getElementById is that we need to add # for id or for class as we do in CSS.

<!DOCTYPE html>
<html>
 <body>
   <h1 id="demo"></h1>
   <script>
     document.querySelector('#demo').innerHTML = 'Hello JavaScript!';
   </script>
 </body>
</html>

Elements By CSS Selectors

We can use the querySelectorAll method to find HTML elements which match a specified CSS Selector(id, class).  It returns an array like object called as NodeList. It can be used in place of getElementsByClassName  or getElementsByTagName.

<!DOCTYPE html>
<html>
 <body>
   <p>Hello JS!</p>
   <p>This example is for you all JS People.</p>
   <div id="demo"></div>
   <script>
     var x = document.querySelectorAll('p');
     document.getElementById('demo').innerHTML =
       x[0].innerHTML + ' ' + x[1].innerHTML;
   </script>
 </body>
</html>

Output with console

JavaScript Code

DOM CSS in Javascript

We can also change the CSS style of an HTML element. Below is an example of the same. Notice, that for CSS property like font-size, we have to use camel case like fontSize.

<!DOCTYPE html>
<html>
 <body>
   <div id="title">Hello JS!</div>
   <p id="para">This example is for you all JS People.</p>
   <script>
     document.querySelector("#title").style.fontSize = "22px";
     document.querySelector("#para").style.color = "blue";
   </script>
 </body>
</html>

OutputJavaScript Code

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

Janani

I have learned many things from this article. It is beneficial for me. Thank you!

Nilesh Chakrabarty

Nice example for beginners.. I m a beginner so this is very helpful for me ... so plz give this type of beginners example..

michael

This is a great introduction to variables in JavaScript! As a beginner to JavaScript, I found this guide very helpful in understanding the basics of variables and how they are used in JavaScript.

qtsinfo it

Thanks for sharing the information, it is very helpful, I hope to get more such beautiful blogs from you.

knowledge-wisdom

You have shared great information with me i appreciate your work!

Suggested Tutorials

Node JS Tutorial

Build various types of web applications,command-line application,etc.
Node JS Tutorial

Build various types of web applications,command-line application,etc....

Read More

Angular JS Tutorial

Introduction: Angular  (What is Angular?)Angular was formerly introduced by Google corporation in 2012 and was considered to be one of the most promising among JavaScript frameworks. It was written completely in JavaScript to separate an application’s logic from DOM manipulation, aiming at dynamic page updates. Angular introduced many powerful features enabling the developer to effortlessly create rich and single-page applications.Topics CoveredThis Angular tutorial will span over eight modules, each module covering numerous individual aspects that you need to gain complete information about Angular. This set of modules serves as an Angular tutorial for beginners along with experienced IT professionals.Here are the topics that will be covered in the Angular tutorial:Get started with Angular.Learn the basics of Angular.Know what Angular Directives.Get an idea of Component Inputs and Outputs of Angular.Know about Forms in Angular.About Services in Angular.Pipes in Angular.HTTP, Routing and Building in Angular.Who can benefit from this tutorial?This Angular tutorial will be helpful to IT professionals such as:Software Developers, Web Application Programmers and IT Professionals Software Architects and Testing Professionals Career aspirants in web development
Angular JS Tutorial

Introduction: Angular  (What is Angular?)Angular was formerly introdu...

Read More