FSD - Experiment - VI - For II-II-CSE-A - 6abcd
FSD - Experiment - VI - For II-II-CSE-A - 6abcd
1. Internal JavaScript:
o The <script> tag inside the HTML <body> contains a JavaScript function named
internalFunction().
o This function modifies the text of the <p> element with the id="message" when the
corresponding button is clicked.
2. External JavaScript:
o The <script src="external-script.js"></script> in the <head> links to the external
JavaScript file.
o The external-script.js file contains the externalFunction(), which also updates the same
paragraph element when its button is clicked.
3. Styling:
o Basic CSS is used to style the buttons for better visual presentation.
Source Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="external-script.js"></script>
<style>
body {
margin: 20px;
.button {
margin: 10px 0;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
.button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<script>
function internalFunction() {
</script>
</body>
</html>
function externalFunction() {
How It Works:
1. Save the external JavaScript code in a file named external-script.js in the same directory as the
HTML file.
2. Open the HTML file in a web browser.
3. Click the "Run Internal JavaScript" button to execute the internal JavaScript function.
4. Click the "Run External JavaScript" button to execute the external JavaScript function.
Result: This program executed successfully and Each button click will update the message displayed in
the <p> element.
Output:
b. Write a program to explain the different ways for displaying output
Description:
Output Methods:
1. alert():
oDisplays a pop-up message to the user.
oUsed for simple notifications or warnings.
oExample: Clicking the "Show Alert Output" button will trigger an alert box.
2. console.log():
o Logs messages to the browser's developer console.
o Useful for debugging and monitoring code behavior.
o Example: Clicking the "Show Console Output" button will log a message to the console
(accessible with developer tools).
3. innerHTML:
o Dynamically updates the content of an HTML element.
o Often used to modify content within specific elements on the page.
o Example: Clicking the "Display Output in Page" button updates the text in the div with
id="innerHTMLOutput".
4. document.write():
o Writes content directly to the document.
o Commonly used in simple examples but not recommended for modern development
(especially after the document is fully loaded).
o Example: Clicking the "Write to Page Directly" button updates the div with
id="documentWriteOutput".
Source Code:
Inner .html
<html>
<body>
<p id="demo"></p>
<script>
</script>
</body>
</html>
Output:
Using innerText
Then use the innerText property to change the inner text of the HTML element:
<html>
<body>
<h1>My Web Page</h1>
<p id="demo"></p>
<script>
document.getElementById("demo").innerText = "Hello World";
</script>
</body>
</html>
Output:
Using document.write()
For testing purposes, it is convenient to use document.write():
<html>
<body>
<h2>My First Web Page</h2>
<p>My first paragraph.</p>
<button type="button" onclick="document.write(5 + 6)">add elements click here</button>
</body>
</html>
Output:
Using window.alert()
You can use an alert box to display data
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
body {
margin: 20px;
line-height: 1.6;
.output {
margin: 10px 0;
padding: 10px;
background-color: #f9f9f9;
</style>
</head>
<body>
<script>
// 1. Alert box
function useAlert() {
// 2. Console.log
function useConsoleLog() {
console.log("This is output displayed in the browser's console.");
function useInnerHTML() {
function useDocumentWrite() {
</script>
</body>
</html>
Output:
How to Use:
Result: This program executed successfully how different output methods are used based on specific use
cases.
Input Methods:
1. Using prompt():
o A pop-up dialog box appears, asking the user to input text.
o The input is captured in a variable and displayed on the page.
o Example: Clicking the "Input via Prompt" button triggers the prompt.
2. Using HTML Form:
o A form contains an input field and a button.
o The value of the input field is retrieved when the button is clicked.
o Example: Typing into the text box and clicking "Submit" displays the input below.
3. Using Event Listener:
o An input event listener is attached to a text field.
o The listener captures and displays the input as the user types.
o Example: As the user types in the "Type something" field, the text is displayed in real-
time.
Source Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript Input Methods</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
line-height: 1.6;
}
.output {
margin: 10px 0;
padding: 10px;
border: 1px solid #ccc;
background-color: #f9f9f9;
}
input, button {
margin: 5px 0;
padding: 8px;
font-size: 16px;
}
</style>
</head>
<body>
<h1>JavaScript Input Methods</h1>
Output:
How to Use:
Result: This program executed successfully how to take user input in various ways, suitable for different
scenarios.
d. Create a webpage which uses prompt dialogue box to ask a voter for his name and age. Display the
information in table format along with either the voter can vote or not.
Description:
a webpage that uses a prompt dialog box to collect a voter's name and age, then displays the information
in a table along with a message indicating whether the voter is eligible to vote.
How It Works:
1. Prompts:
o The checkVoter() function uses the prompt() method to collect the voter's name and age.
o If the user enters invalid data (e.g., blank name or non-numeric age), an alert is displayed,
and the process ends.
2. Eligibility Check:
o If the voter's age is 18 or above, they are considered eligible to vote; otherwise, they are
not eligible.
o Eligibility is determined using a simple conditional check (age >= 18).
3. Dynamic Table:
o The voter's details (name, age, and eligibility status) are dynamically added as a new row
to the table using innerHTML.
4. Styling:
o The table and eligibility messages are styled using CSS.
o Eligible voters are displayed in green, while those not eligible are displayed in red.
Source Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voter Eligibility Check</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
line-height: 1.6;
}
table {
width: 50%;
border-collapse: collapse;
margin: 20px 0;
}
th, td {
border: 1px solid #ccc;
padding: 10px;
text-align: left;
}
th {
background-color: #f4f4f4;
}
.eligible {
color: green;
font-weight: bold;
}
.not-eligible {
color: red;
font-weight: bold;
}
</style>
</head>
<body>
<h1>Voter Eligibility Check</h1>
<button onclick="checkVoter()">Enter Voter Details</button>
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Eligibility</th>
</tr>
</thead>
<tbody id="voterTableBody">
<!-- Voter information will be appended here -->
</tbody>
</table>
<script>
function checkVoter() {
// Prompt the user for name and age
const name = prompt("Enter your name:");
const age = prompt("Enter your age:");
Output:
How to Use:
Result: This program executed successfully and repeat this process for multiple voters to see their details
added to the table.