Skip to main content

Python type() Function

When learning Python, you will work with different types of data, such as numbers, text, decimal values, and Boolean values. Python provides several built-in functions to help you work with these data types. One of the most useful functions is the type() function . The type() function tells you the data type of a value or variable. It helps you understand what kind of data Python is working with and can make debugging your programs easier. For example, consider these variables: age = 20 price = 99.50 name = "Rahul" is_student = True Although all four variables store data, their data types are different. Python allows us to check these types using type() . Let's understand how it works with simple examples. 1. What Is the type() Function in Python? The type() function is a built-in Python function that returns information about the type of an object. In beginner-level programming, you can use it to check whether a value is an integer, a floating-point number, a string,...

JavaScript DOM (Document Object Model) – Making Webpages Dynamic

So far, we have learned:

  • HTML → Structure of webpages

  • CSS → Styling of webpages

  • JavaScript → Programming and logic

  • Variables, Functions, Arrays, and Objects

However, JavaScript becomes truly powerful when it interacts with HTML elements on a webpage.

For example:

  • Change text when a button is clicked

  • Change colors dynamically

  • Hide or show elements

  • Validate forms

  • Create animations

This interaction is possible through the DOM (Document Object Model).

In this article, we will learn:

  • What DOM is

  • How DOM works

  • DOM tree structure

  • Selecting HTML elements

  • Changing content

  • Changing styles

  • Creating elements

  • Removing elements

  • Real-world examples


What is DOM?

DOM stands for:

Document Object Model

The DOM is a programming interface that represents an HTML document as a tree of objects.

JavaScript uses the DOM to:

  • Access HTML elements

  • Modify content

  • Modify styles

  • Add or remove elements

  • Respond to user actions


Why DOM is Needed

Without DOM:

HTML remains static.

Example:

<h1>Hello World</h1>

The text never changes.


With DOM:

JavaScript can change it dynamically.

Example:

document.getElementById(
"title"
).innerHTML =
"Welcome";

The webpage updates instantly.


How DOM Works

When a webpage loads:

HTML File
      ↓
Browser
      ↓
DOM Tree Created
      ↓
JavaScript Accesses Elements

Example HTML

<html>

<body>

<h1>Hello</h1>

</body>

</html>

DOM Tree:

Document
   │
   └── html
         │
         └── body
                │
                └── h1
                     │
                  Hello

Each HTML element becomes an object.


The Document Object

The entire webpage is represented by:

document

It is the root of the DOM.

Example:

document.write(
"Hello"
);

Selecting HTML Elements

Before changing an element, JavaScript must find it.

Common methods:

MethodPurpose
getElementById()Select by id
getElementsByClassName()Select by class
getElementsByTagName()Select by tag
querySelector()Select first matching element
querySelectorAll()Select all matching elements

getElementById()

Most commonly used method.


HTML

<h1 id="title">

Hello World

</h1>

JavaScript

document.getElementById(
"title"
);

Selects the heading.


Changing Content

Using:

innerHTML

Example

HTML:

<h1 id="title">

Hello World

</h1>

JavaScript:

document.getElementById(
"title"
).innerHTML =
"Welcome";

Output:

Welcome

Complete Program

<!DOCTYPE html>

<html>

<body>

<h1 id="title">

Hello World

</h1>

<script>

document.getElementById(
"title"
).innerHTML =
"Welcome";

</script>

</body>

</html>

Changing Text on Button Click

HTML:

<h1 id="demo">

Hello

</h1>

<button
onclick="changeText()">

Click Me

</button>

JavaScript:

function changeText()
{
    document.getElementById(
    "demo"
    ).innerHTML =
    "Welcome";
}

Output

Before Click:

Hello

After Click:

Welcome

Changing Styles

DOM can modify CSS.


Example

HTML:

<h1 id="title">

Hello

</h1>

JavaScript:

document.getElementById(
"title"
).style.color =
"red";

Output:

Heading becomes red

Changing Font Size

document.getElementById(
"title"
).style.fontSize =
"40px";

Multiple Style Changes

let heading =
document.getElementById(
"title"
);

heading.style.color =
"blue";

heading.style.backgroundColor =
"yellow";

Changing Images

HTML:

<img
id="image"
src="image1.jpg">

JavaScript:

document.getElementById(
"image"
).src =
"image2.jpg";

Image changes instantly.


Reading User Input

HTML:

<input
type="text"
id="name">

JavaScript:

let value =
document.getElementById(
"name"
).value;

Gets user input.


Example

<input
type="text"
id="name">

<button
onclick="show()">

Show

</button>
function show()
{
    let value =
    document.getElementById(
    "name"
    ).value;

    alert(value);
}

Creating New Elements

DOM can create elements dynamically.


Syntax

document.createElement(
"tag"
);

Example

let para =
document.createElement(
"p"
);

Creates a paragraph.


Adding Content

para.innerHTML =
"New Paragraph";

Adding Element to Page

document.body.appendChild(
para
);

Complete Example

let para =
document.createElement(
"p"
);

para.innerHTML =
"Hello DOM";

document.body.appendChild(
para
);

Output:

Hello DOM

appears on webpage.


Removing Elements

HTML:

<p id="demo">

Hello

</p>

JavaScript:

document.getElementById(
"demo"
).remove();

Paragraph disappears.


querySelector()

Selects first matching element.

Example:

document.querySelector(
"h1"
);

Selects first heading.


querySelectorAll()

Selects all matching elements.

Example:

document.querySelectorAll(
"p"
);

Selects all paragraphs.


Example

HTML:

<p>One</p>

<p>Two</p>

JavaScript:

let para =
document.querySelectorAll(
"p"
);

Returns both paragraphs.


Event Handling

Events occur when users interact with a webpage.

Examples:

  • Click

  • Mouse movement

  • Keyboard press

  • Form submission


Common Events

EventDescription
onclickMouse click
onmouseoverMouse hover
onmouseoutMouse leave
onkeydownKey press
onchangeValue changed

Click Event Example

HTML:

<button
onclick="show()">

Click

</button>

JavaScript:

function show()
{
    alert(
    "Button Clicked"
    );
}

Mouse Over Example

<h1
onmouseover=
"this.style.color='red'">

Move Mouse Here

</h1>

Form Validation Example

<input
type="text"
id="username">

<button
onclick="validate()">

Submit

</button>
function validate()
{
    let name =
    document.getElementById(
    "username"
    ).value;

    if(name=="")
    {
        alert(
        "Enter Name"
        );
    }
}

Complete DOM Program

<!DOCTYPE html>

<html>

<body>

<h1 id="title">

Hello World

</h1>

<button
onclick="changeText()">

Click Me

</button>

<script>

function changeText()
{
    document.getElementById(
    "title"
    ).innerHTML =
    "Welcome to DOM";

    document.getElementById(
    "title"
    ).style.color =
    "blue";
}

</script>

</body>

</html>

Output

Before Click:

Hello World

After Click:

Welcome to DOM

and color changes to blue.


Real-Life Applications of DOM

DOM is used in:

  • Login systems

  • Registration forms

  • Online shopping carts

  • Games

  • Social media feeds

  • Live chat systems

Platforms like Facebook, YouTube, and Amazon rely heavily on DOM manipulation for dynamic user interfaces.


Common Mistakes Beginners Make

Wrong ID Name

HTML:

<h1 id="title">

JavaScript:

getElementById(
"heading"
)

Returns:

null

Running JavaScript Before HTML Loads

Wrong:

document.getElementById(
"title"
);

before element exists.


Forgetting Quotes

Wrong:

getElementById(title)

Correct:

getElementById("title")

Advantages of DOM

  • Dynamic webpages

  • Interactive user interfaces

  • Real-time updates

  • Better user experience

  • Powerful event handling


Conclusion

The DOM is the bridge between JavaScript and HTML. It allows JavaScript to access, modify, create, and remove webpage elements dynamically.

Comments

Popular posts from this blog

Understanding Comments in Java: A Beginner’s Guide

When learning Java programming, one of the first concepts you’ll come across is comments . Comments may not affect how a program runs, but they play a crucial role in making code more understandable, readable, and maintainable. Let’s dive deeper into what comments are, why they’re important, and the different types of comments in Java. What are Comments in Java? A comment in Java is a piece of text written within a program that is ignored by the compiler . In other words, comments are notes for humans, not instructions for machines. They help developers explain the purpose of code, describe logic, or temporarily disable certain lines of code without deleting them. Why are Comments Important? ๐Ÿ“Œ Readability – Comments make code easier to understand for you and others. ๐Ÿ“Œ Maintenance – When you revisit your code after months, comments remind you what you were doing. ๐Ÿ“Œ Collaboration – In team projects, comments explain code to fellow developers. ๐Ÿ“Œ Debugging – You c...

Java Program to Check Voting Eligibility

In this program, we’ll take the user’s age as input and check whether the person is eligible to vote or not. In India, the minimum age required to vote is 18 years . ๐Ÿ’ป Program Code // Java Program to Check Voting Eligibility import java.util.Scanner; public class VotingEligibility { public static void main(String[] args) { // Create Scanner object to take input from user Scanner sc = new Scanner(System.in); // Ask user to enter age System.out.print("Enter your age: "); int age = sc.nextInt(); // Check eligibility if (age >= 18) { System.out.println("You are eligible to vote."); } else { System.out.println("You are not eligible to vote yet."); System.out.println("You can vote after " + (18 - age) + " year(s)."); } sc.close(); // Close the scanner } } ๐Ÿง  Explanation import java.util.Scanner; → Imp...

Java Program to Calculate Total Marks and Average of Five Subjects

๐Ÿ’ก Program Explanation In this program: The user will input marks of five subjects . The program will calculate the total marks and average . It will then display both results clearly. ๐Ÿง‘‍๐Ÿ’ป Java Code: // Java Program to Calculate Total Marks and Average of Five Subjects import java.util.Scanner; // Importing Scanner class for user input public class TotalAndAverage { public static void main(String[] args) { // Creating Scanner object to take input from user Scanner sc = new Scanner(System.in); // Declaring variables for five subjects double subject1, subject2, subject3, subject4, subject5; // Taking marks input for each subject System.out.print("Enter marks of Subject 1: "); subject1 = sc.nextDouble(); System.out.print("Enter marks of Subject 2: "); subject2 = sc.nextDouble(); System.out.print("Enter marks of Subject 3: "); subject3 = sc.nextDo...