Related Topics
Introduction
Html page 1
Html page 2
Html page3
Html page4
HTML Elements and structure
Html page 5
Html page 6
Html page 7
HTML Headings and Paragraphs
Html page 8
Html page 9
Html page 10
HTML Lists and Tables
Html page 11
Html page 12
Html page 13
HTML Forms and Input Fields
Html page 14
Html page 15
Html page 16
HTML Images and Media
Html page 17
Html page 18
HTML Links and Anchors
Html page 19
Html page 20
Html page 21
HTML Styles and Formatting
Html page 22
HTML Semantic Elements
Html page 23
Html page 24
HTML Attributes
Html page 25
Html page 26
HTML JavaScript Integration
Html page 27
Html page 28
Html page 29
Html page 30
HTML Document and Browser Support
Html page 31
Html page 32
HTML5 New Elements and Attributes
Html page 33
Html page 34
Html page 35
Html page 36
HTML Accessibility and Web Standards
Html page 37
Html page 38
Html page 39
HTML Responsive Design and Mobile Devices.
Html page 40
Html page 41
Html page 42
Introduction
Data Structure Page 1
Data Structure Page 2
Data Structure Page 3
Data Structure Page 4
Data Structure Page 5
Data Structure Page 6
Data Structure Page 7
Data Structure Page 8
String
Data Structure Page 9
Data Structure Page 10
Data Structure Page 11
Data Structure Page 12
Data Structure Page 13
Array
Data Structure Page 14
Data Structure Page 15
Data Structure Page 16
Data Structure Page 17
Data Structure Page 18
Linked List
Data Structure Page 19
Data Structure Page 20
Stack
Data Structure Page 21
Data Structure Page 22
Queue
Data Structure Page 23
Data Structure Page 24
Tree
Data Structure Page 25
Data Structure Page 26
Binary Tree
Data Structure Page 27
Data Structure Page 28
Heap
Data Structure Page 29
Data Structure Page 30
Graph
Data Structure Page 31
Data Structure Page 32
Searching Sorting
Data Structure Page 33
Hashing Collision
Data Structure Page 35
Data Structure Page 36

JAVASCRIPT
<title>Event Handling Example</title>
<button id="myButton">Click Me</button>
<div id="myDiv">Hover over me</div>
// Selecting the elements
const button = document.getElementById('myButton');
const div = document.getElementById('myDiv');
// Click event handler
function handleClick(event) {
console.log('Button was clicked!');
// Add your code here to perform actions when the button is clicked.
}
// Hover event handler
function handleHover(event) {
console.log('Mouse is over the div!');
// Add your code here to perform actions when the mouse hovers over the div.
}
// Attaching the event listeners
button.addEventListener('click', handleClick);
div.addEventListener('mouseover', handleHover);
In this example, we have a button and a div element, and we attach event listeners to them to handle the click and hover events, respectively. When you click the button, the handleClick
function will be executed, and when you hover over the div, the handleHover
function will run.
Remember to always include your JavaScript code after the DOM elements have been loaded (either by placing the script at the bottom of the body or using the DOMContentLoaded
event) to ensure that the elements are accessible when attaching the event listeners.
const bodyElement = document.body; // Accessing the body element of the document
const titleElement = document.getElementById('title'); // Accessing an element with id="title"
Element: The
Element
object represents an individual HTML element within the DOM. In other words, any HTML tag, such as<div>
,<p>
,<a>
, etc., corresponds to anElement
object when parsed into the DOM tree.
Key Characteristics:
The
Element
interface extends theNode
interface (hence, it has all the properties and methods of theNode
).It provides access to properties and methods specific to HTML elements, such as
innerHTML
,classList
,style
, etc.Elements can be selected using methods like
getElementById
,querySelector
, or by accessing properties likedocument.body
, etc.
Example:
<div id="myDiv">This is a div element</div>
const myDivElement = document.getElementById('myDiv'); // Accessing the div element
Node: The
Node
object represents a single node in the DOM tree. A node can be an element node, a text node, an attribute node, or any other type of node defined in the DOM. Both theDocument
andElement
objects are subclasses of theNode
object, meaning they inherit its properties and methods.
Key Characteristics:
The
Node
interface provides basic properties and methods for all types of nodes, such asnodeType
,nodeName
,nodeValue
,appendChild
,removeChild
, etc.Nodes can be elements, text, comments, document type declarations, etc.
Example:
<p>This is a <em>paragraph</em> element</p>
const paragraphNode = document.querySelector('p'); // Accessing the paragraph element node
const textNode = paragraphNode.firstChild; // Accessing the text node containing "This is a "
const emphasisNode = paragraphNode.querySelector('em'); // Accessing the em (emphasis) element node
In summary, the Document
represents the entire HTML document, the Element
represents individual HTML elements, and the Node
is the basic unit that forms the DOM tree, including elements and other types of nodes. Elements are nodes, but not all nodes are elements.
<title>Accessing Element Attributes</title>
<img id="myImage" src="image.jpg" alt="A beautiful image">
<a id="myLink" href="https://www.example.com" target="_blank">Visit Example</a>
// Accessing attributes using getAttribute()
const imageElement = document.getElementById('myImage');
const linkElement = document.getElementById('myLink');
const imageSrc = imageElement.getAttribute('src');
const linkHref = linkElement.getAttribute('href');
console.log(`Image source: ${imageSrc}`);
console.log(`Link href: ${linkHref}`);
Directly accessing element properties:
Some attributes have corresponding properties that can be accessed directly from the element object. For example, the src
attribute of an <img>
tag has a corresponding src
property.
<title>Accessing Element Properties</title>
<img id="myImage" src="image.jpg" alt="A beautiful image">
<a id="myLink" href="https://www.example.com" target="_blank">Visit Example</a>
// Accessing attributes using element properties
const imageElement = document.getElementById('myImage');
const linkElement = document.getElementById('myLink');
const imageSrc = imageElement.src;
const linkHref = linkElement.href;
console.log(`Image source: ${imageSrc}`);
console.log(`Link href: ${linkHref}`);
Note that not all attributes have corresponding properties. In such cases, you should use the getAttribute()
method to access the attribute values.
Using either method, you can read the values of attributes associated with an element in the DOM, which allows you to retrieve important information from HTML elements, such as image sources, links, data attributes, and more.
<title>Setting Styles with JavaScript</title>
/* Define a CSS class */
.highlight {
background-color: yellow;
color: black;
font-weight: bold;
}
<p id="myParagraph">This is a paragraph.</p>
<button id="myButton">Click Me</button>
// Select the elements
const paragraphElement = document.getElementById('myParagraph');
const buttonElement = document.getElementById('myButton');
// Set styles using the style property
paragraphElement.style.backgroundColor = 'blue';
paragraphElement.style.color = 'white';
paragraphElement.style.fontSize = '18px';
// You can also set styles using a CSS class
buttonElement.classList.add('highlight');
In the example above, we have a paragraph element and a button. We use JavaScript to set the styles for the paragraph and apply a CSS class to the button element.
To set styles using the style
property:
Access the element you want to style using methods like
getElementById
,querySelector
, etc.Use the
style
property of the element to access its CSS properties.Set the desired CSS properties as if you were modifying the inline style attribute of the element in HTML.
To set styles using a CSS class:
Define a CSS class with the desired styles in a
<style>
tag or external stylesheet.Use JavaScript to add the class to the
classList
property of the element you want to style. TheclassList.add()
method adds the specified class to the element.
By using JavaScript to set styles, you can dynamically change the appearance of elements on your web page based on various conditions or user interactions.
<title>Dynamic Element Creation and Styling</title>
<button id="createButton">Create Box</button>
<div id="container"></div>
const createButton = document.getElementById('createButton');
const container = document.getElementById('container');
createButton.addEventListener('click', () => {
// Create a new div element
const newBox = document.createElement('div');
// Set styles for the new div element
newBox.style.width = '100px';
newBox.style.height = '100px';
newBox.style.backgroundColor = 'red';
newBox.style.margin = '10px';
// Add the new element to the container
container.appendChild(newBox);
});
In this example, clicking the “Create Box” button will create a new <div>
element with specified styles (width, height, background color, and margin) and add it to the container div.
Modifying Existing Element Styles:
To modify styles of existing elements:
<title>Modifying Element Styles</title>
<div id="myDiv">This is a div element.</div>
const myDiv = document.getElementById('myDiv');
// Modifying the existing styles
myDiv.style.backgroundColor = 'blue';
myDiv.style.color = 'white';
myDiv.style.fontSize = '18px';
In this example, the JavaScript code modifies the background color, text color, and font size of the existing <div>
element with the id “myDiv”.
By using the style
property, you can directly access and modify the inline styles of elements. However, it’s worth noting that inline styles can be overwritten by other styles defined in CSS, so using classes and CSS rules is often a better practice for more complex styling scenarios.
To apply multiple styles to an element, you can also use CSS classes. Here’s how you can add a class to an element dynamically:
const element = document.getElementById('myElement');
element.classList.add('myClass');
With this approach, you define the styles in a CSS class and toggle the class on or off for elements as needed. This allows you to manage styles more efficiently and separate them from your JavaScript code.




Popular Category
Topics for You
Introduction
Html page 1
Html page 2
Html page3
Html page4
HTML Elements and structure
Html page 5
Html page 6
Html page 7
HTML Headings and Paragraphs
Html page 8
Html page 9
Html page 10
HTML Lists and Tables
Html page 11
Html page 12
Html page 13
HTML Forms and Input Fields
Html page 14
Html page 15
Html page 16
HTML Images and Media
Html page 17
Html page 18
HTML Links and Anchors
Html page 19
Html page 20
Html page 21
HTML Styles and Formatting
Html page 22
HTML Semantic Elements
Html page 23
Html page 24
HTML Attributes
Html page 25
Html page 26
HTML JavaScript Integration
Html page 27
Html page 28
Html page 29
Html page 30
HTML Document and Browser Support
Html page 31
Html page 32
HTML5 New Elements and Attributes
Html page 33
Html page 34
Html page 35
Html page 36
HTML Accessibility and Web Standards
Html page 37
Html page 38
Html page 39
HTML Responsive Design and Mobile Devices.
Html page 40
Html page 41
Html page 42
Introduction
Data Structure Page 1
Data Structure Page 2
Data Structure Page 3
Data Structure Page 4
Data Structure Page 5
Data Structure Page 6
Data Structure Page 7
Data Structure Page 8
String
Data Structure Page 9
Data Structure Page 10
Data Structure Page 11
Data Structure Page 12
Data Structure Page 13
Array
Data Structure Page 14
Data Structure Page 15
Data Structure Page 16
Data Structure Page 17
Data Structure Page 18
Linked List
Data Structure Page 19
Data Structure Page 20
Stack
Data Structure Page 21
Data Structure Page 22
Queue
Data Structure Page 23
Data Structure Page 24
Tree
Data Structure Page 25
Data Structure Page 26
Binary Tree
Data Structure Page 27
Data Structure Page 28
Heap
Data Structure Page 29
Data Structure Page 30
Graph
Data Structure Page 31
Data Structure Page 32
Searching Sorting
Data Structure Page 33
Hashing Collision
Data Structure Page 35
Data Structure Page 36
Go through our study material. Your Job is awaiting.