JavaScript
90 Best JavaScript Certification Courses in 2021
JavaScript is the most popular client-side scripting language supported by all browsers. It is...
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>
JavaScript is the most popular client-side scripting language supported by all browsers. It is...
What is Constructor in Java? CONSTRUCTOR is a special method that is used to initialize a newly...
In this tutorial, you will learn- How to use Conditional Statements Different Types of Conditional...
What is Quick Sort? Quick Sort algorithm follows Divide and Conquer approach. It divides elements...
What is Interface? The interface is a blueprint that can be used to implement a class. The...
What are Cookies? A cookie is a piece of data that is stored on your computer to be accessed by...