In: Computer Science
For this assignment, you are going to build upon several skills that you've learned:
Set up
Create the assignment in a "week6" folder with the typical files:
This is the standard structure that we'll use in all assignments.
Here is the HTML to use for this assignment. Change the meta author tag to include your name!
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Working with Objects and NodeLists</title> <meta name="description" content="We want to update an HTML page with information from a data object."> <meta name="author" content="C SCI 212 Student"> <!-- link to external CSS file --> <link rel="stylesheet" href="css/styles.css?v=1.0"> </head> <body> <!-- Content of the page goes here. --> <h1>Introduction</h1> <section class="intro"> <h2>Introduction</h2> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> </section> <section class="student-info"> <h2>Student Information</h2> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> </section> <!-- link to external JS file --> <script src="js/scripts.js"></script> </body> </html>
The Problem
We have a JavaScript object that contains student information.
We want to update the HTML page with information from the JavaScript object.
Instructions
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<title>Working with Objects and
NodeLists</title>
<meta name="description" content="We want to
update an HTML page with information from a data object.">
<meta name="author" content="C SCI 212
Student">
<!-- link to external CSS file
-->
<link rel="stylesheet"
href="css/styles.css?v=1.0">
</head>
<body>
<!-- Content of the page goes here.
-->
<h1>Introduction</h1>
<section class="intro">
<h2>Introduction</h2>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
</section>
<section class="student-info">
<h2>Student
Information</h2>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
</section>
<!-- link to external JS file -->
<script
src="js/scripts.js"></script>
</body>
</html>
style.css
body {/*for body of html*/
background-color: lightblue;
}
h1 {/*for h1 tag of html*/
color: white;
text-align: center;
}
p {/*for paragraph tah of html*/
font-family: verdana;
font-size: 20px;
}
scripts.js
function studentInfo(){
var myNodelist = document.querySelectorAll("p");//get
total nodelist of <p> tag in html
var length=myNodelist.length;//get total length of
<p> tag in html
for(let i = 7; i < length; i++){//run for loop
for last 7 <p> tags in studentInfo section
myNodelist[i].innerHTML="Student
paragraph with index "+i;//set data inside <p> tag
}
}
function intro(){
var myNodelist = document.querySelectorAll("p");//get
total nodelist of <p> tag in html
var length=myNodelist.length;//get total length of
<p> tag in html
for(let i = 0; i < 7; i++){//run for loop for
first 7 <p> tags in intro section
myNodelist[i].innerHTML="Intro
paragraph with index "+i;//set data inside <p> tag
}
}
window.onload = function() {
//intro();//call function
intro
studentInfo();//call function studentInfo
}
Output