javascript
Copy code
// Accessing the window object
console.log(window);
// Accessing the document object inside the window object
console.log(window.document);
// Accessing the location object inside the window object
console.log(window.location);
// Accessing the history object inside the window object
console.log(window.history);
// Accessing the navigator object inside the window object
console.log(window.navigator);
<!-- HTML form -->
<form>
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<button type="submit">Submit</button>
</form>
// Getting the form by its ID
const form = document.getElementById('my-form');
// Listening for the submit event
form.addEventListener('submit', event => {
// Preventing the form from submitting
event.preventDefault();
// Getting the value of the name input
const nameInput = document.getElementById('name');
const name = nameInput.value;
// Displaying the name in a message
const message = `Hello, ${name}!`;
alert(message);
});
<!-- HTML button -->
<button id="my-button">Click me!</button>
// Adding a click event listener using jQuery
$('#my-button').click(() => {
alert('You clicked the button!');
});
<!-- HTML form -->
<form>
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<button type="submit">Submit</button>
</form>
// Getting the form by its ID
const form = document.getElementById('my-form');
// Listening for the submit event
form.addEventListener('submit', event => {
// Preventing the form from submitting
event.preventDefault();
// Getting the value of the name input
const nameInput = document.getElementById('name');
const name = nameInput.value;
// Displaying the name in a message
const message = `Hello, ${name}!`;
alert(message);
});
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery Example</title>
<style>
.box {
width: 100px;
height: 100px;
background-color: red;
margin: 10px;
}
</style>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<button id="animateBtn">Animate</button>
<button id="toggleBtn">Toggle</button>
<div class="box"></div>
<p id="content">This is some sample content.</p>
<script src="script.js"></script>
</body>
</html>
$("#animateBtn").click(function() {
$(".box").animate({
width: "200px",
height: "200px",
opacity: 0.5
}, 1000, function() {
// Animation complete
console.log('Animation completed');
});
});
$("#toggleBtn").click(function() {
$(".box").toggle(1000);
});
$(".box").hover(
function() {
$(this).css("background-color", "blue");
},
function() {
$(this).css("background-color", "red");
}
);
$("#content").text("This is the updated content using .text() method.");
$("#content").html("<strong>This is the updated content using .html() method.</strong>");
$("body").append("<p>This is a new paragraph added using .append() method.</p>");