Java Tutorials
C++ Vs JAVA: What’s the Difference?
What is the C++ language? C++ is a computer programming language that contains the feature of C...
JavaScript can access all the elements in a webpage making use of Document Object Model (DOM). In fact, the web browser creates a DOM of the webpage when the page is loaded. The DOM model is created as a tree of objects like this:
Using DOM, JavaScript can perform multiple tasks. It can create new elements and attributes, change the existing elements and attributes and even remove existing elements and attributes. JavaScript can also react to existing events and create new events in the page.
Try this Example yourself:
<html> <head> <title>DOM!!!</title> </head> <body> <h1 id="one">Welcome</h1> <p>This is the welcome message.</p> <h2>Technology</h2> <p>This is the technology section.</p> <script type="text/javascript"> var text = document.getElementById("one").innerHTML; alert("The first heading is " + text); </script> </body> </html>
getElementsByTagName: To access elements and attributes using tag name. This method will return an array of all the items with the same tag name.
Try this Example yourself:
<html> <head> <title>DOM!!!</title> </head> <body> <h1>Welcome</h1> <p>This is the welcome message.</p> <h2>Technology</h2> <p id="second">This is the technology section.</p> <script type="text/javascript"> var paragraphs = document.getElementsByTagName("p"); alert("Content in the second paragraph is " + paragraphs[1].innerHTML); document.getElementById("second").innerHTML = "The orginal message is changed."; </script> </body> </html>
document.getElementById(id).onclick=function() { lines of code to be executed }
OR
document.getElementById(id).addEventListener("click", functionname)
Try this Example yourself:
<html> <head> <title>DOM!!!</title> </head> <body> <input type="button" id="btnClick" value="Click Me!!" /> <script type="text/javascript"> document.getElementById("btnClick").addEventListener("click", clicked); function clicked() { alert("You clicked me!!!"); } </script> </body> </html>
What is the C++ language? C++ is a computer programming language that contains the feature of C...
In this JavaScript Unit Testing tutorial, we will learn: What is JavaScript? JavaScript is a...
What is Command Line Argument in Java? Command Line Argument in Java is the information that is...
You can use JavaScript code in two ways. You can either include the JavaScript code internally within...
Example#1: JavaScript Multiplication Table Create a simple multiplication table asking the user...
What is Armstrong Number ? In an Armstrong Number, the sum of power of individual digits is equal...