Program 7,9,10
Program 7,9,10
Index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JSON Operations</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>JSON Operations</h1>
<div class="container">
<!-- Textarea for JSON input -->
<textarea id="jsonInput" placeholder="Enter JSON here (e.g., [{'name':
'John'}, {'name': 'Jane'}])"></textarea>
<script src="script.js"></script>
</body>
</html>
Style.css
h1 {
text-align: center;
margin-bottom: 20px;
}
textarea {
width: 80%;
height: 150px;
margin-right: 20px;
padding: 10px;
font-size: 16px;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
padding: 10px;
font-size: 14px;
border: 1px solid #ccc;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #f0f0f0;
}
#hashInput {
padding: 10px;
font-size: 16px;
border: 1px solid #ccc;
border-radius: 5px;
width: 70%;
}
/* Output section */
.output {
margin-top: 30px;
}
pre {
background: #f8f8f8;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
white-space: pre-wrap;
word-wrap: break-word;
}
.error {
color: red;
}
Script.js
OUTPUT:
1. Convert JSON to Object
PROGRAM09:
Html code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Append and Animate</title>
<link rel="stylesheet" href="style1.css">
</head>
<body>
<div class="container">
<p id="my-paragraph">This is a paragraph.</p>
<ul id="my-list">
<li>List item 1</li>
<li>List item 2</li>
</ul>
<textarea id="user-input" placeholder="Enter your content
here..."></textarea>
<br>
<button id="appendContent">Append Content</button>
<button id="animateDiv">Animate Div</button>
<div id="animated-div" class="animated-div"></div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="script.js"></script>
</body>
</html>
Script.js: code
$(document).ready(function () {
// Append content to paragraph and list
$('#appendContent').click(function () {
// Get the content from the text area
var userContent = $('#user-input').val();
Style1.css: code
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4; /* Light background for better contrast */
color: #333; /* Darker text for readability */
margin: 0; /* Remove default margin */
padding: 20px; /* Add padding to the body */
}
.container {
max-width: 600px; /* Limit the width of the container */
margin: 20px auto; /* Center the container */
padding: 20px; /* Add padding inside the container */
background-color: #fff; /* White background for the container */
border-radius: 8px; /* Rounded corners */
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); /* Subtle shadow for depth */
}
#my-paragraph {
margin-bottom: 10px;
font-size: 1.2em; /* Slightly larger font size */
}
#my-list {
margin-bottom: 10px;
padding-left: 20px; /* Indent list items */
}
textarea {
margin-top: 10px;
width: 95%; /* Full width for better usability */
height: 50px;
padding: 10px; /* Padding for better text input */
border: 1px solid #ccc; /* Light border */
border-radius: 5px; /* Rounded corners */
resize: none; /* Disable resizing */
}
button {
margin-right: 10px;
padding: 10px 15px; /* Increased padding for better click area */
background-color: #007BFF; /* Primary button color */
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s ease; /* Smooth transition for hover effect
*/
}
button:hover {
background-color: #0056b3; /* Darker shade on hover */
}
.animated-div {
width: 100%; /* Full width for better visibility */
height: 100px;
background-color: #4CAF50; /* Initial color */
margin-top: 20px;
transition: background-color 0.5s ease; /* Smooth transition for color change
*/
border-radius: 5px; /* Rounded corners */
}
.animate-state1 {
background-color: #FF5733; /* Change color after animation */
}
.animate-state2 {
background-color: #6dff33; /* Another state */
}
OUTPUT
PROGRAM10: CODE
Index.html:
<!DOCTYPE html>
<head>
<title>AJAX Examples | vtucode</title>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f9;
}
h1 {
text-align: center;
color: #333;
padding: 20px 0;
}
#content {
flex-direction: column;
display: flex;
max-width: 600px;
margin: 20px auto;
padding: 20px;
border: 1px solid #ddd;
border-radius: 8px;
background-color: #fff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
button {
display: inline-block;
padding: 10px 15px;
margin: 12px;
border: none;
border-radius: 5px;
background-color: #007bff;
color: #fff;
font-size: 16px;
cursor: pointer;
transition: box-shadow 0.3s;
}
button:hover {
box-shadow: 0 0 0 2px #fff, 0 0 0 4px #007bff;
}
button:focus {
box-shadow: 0 0 0 2px #fff, 0 0 0 4px #007bff;
}
#output {
display: none;
margin-top: 20px;
padding: 10px;
border-radius: 5px;
white-space: pre-wrap;
max-height: 300px;
overflow-y: auto;
}
#output.plain-ajax {
background-color: #f0f8ff;
border: 1px solid #b0c4de;
}
#output.jquery-ajax {
background-color: #f5fffa;
border: 1px solid #98fb98;
}
#output.jquery-json {
background-color: #fffaf0;
border: 1px solid #ffd700;
}
#output.parse-json {
background-color: #fff0f5;
border: 1px solid #ff69b4;
}
</style>
</head>
<body>
<h1>AJAX Examples</h1>
<div id="content">
<button id="plain-ajax-btn">Load Text (Plain AJAX)</button>
<button id="jquery-ajax-btn">Load Text (jQuery AJAX)</button>
<button id="jquery-json-btn">Load JSON (jQuery getJSON)</button>
<button id="parse-json-btn">Load and Parse JSON (jQuery get)</button>
<div id="output"></div>
</div>
<script>
function showOutput(className) {
const output = document.getElementById('output');
output.className = className;
output.style.display = 'block';
}
document.getElementById('plain-ajax-btn').addEventListener('click',
function () {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'textfile.txt', true);
xhr.onload = function () {
if (xhr.status === 200) {
document.getElementById('output').innerText =
xhr.responseText;
} else {
document.getElementById('output').innerText = 'Error loading
file.';
}
showOutput('plain-ajax');
};
xhr.send();
});
$('#jquery-ajax-btn').on('click', function () {
$.ajax({
url: 'textfile.txt',
method: 'GET',
success: function (data) {
$('#output').text(data);
},
error: function () {
$('#output').text('Error loading file.');
}
}).always(function () {
showOutput('jquery-ajax');
});
});
$('#jquery-json-btn').on('click', function () {
$.getJSON('data.json')
.done(function (data) {
$('#output').text(JSON.stringify(data, null, 2));
})
.fail(function () {
$('#output').text('Error loading JSON file.');
})
.always(function () {
showOutput('jquery-json');
});
});
$('#parse-json-btn').on('click', function () {
$.get('data.json')
.done(function (data) {
try {
let jsonData;
</html>
Data.json:
{
"name": "John Doe",
"age": 30,
"city": "New York"
}
TEXTFILE.TXT: