In: Computer Science
Exercise 1
(a) Create a HTML file that uses createElement and appendChild to
dynamically insert three paragraphs when
a button is clicked.
(b) Create a HTML file that includes JavaScript that is similar
to:
let recs = [“my item …1”,”my item…2”, …]
i.e. an array that contains several CSV item records. When the user
clicks a button, each array element will be
rendered as a list element. Use a HTML list. Use createElement and
appendChild.
(c) Redo your createTable code from last week’s lab so that it uses
createElement and appendChild rather than
setting an innerHTML property.
Exercise 2
(a) Add an additional button to your 1(c) code. When the user
clicks this additional button an additional table
column appears that contains a row number. Use createElement and
appendChild to achieve this. Do not use
styles or other techniques to hide/show this additional
column.
(b) Rework your 2(b) code so that the additional column contains a
checkbox for each row. When the
additional column is created an additional filter button is also
created. When the user clicks this additional
filter button the table is restricted to those rows the user has
ticked. Another button may be used to reset this
filter.
Dear Student ,
As per the requirement submitted above , kindly find the below solution.
Here a new web page with name "Exercise1B.html" is created, which contains following code.
Exercise1B.html :
<!DOCTYPE html>
<html lang="en">
<head>
<!-- title for web page -->
<title>Exercise 1</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<!-- button to generate para -->
<input type="button" value="click me to create para" onclick="generatePara()">
<!-- button to generate list -->
<input type="button" value="click me to create list" onclick="generateList()">
<!-- <script> is used for javascript -->
<script>
//function to generate paragraphs
function generatePara()
{
//using for loop
for(var i=0;i<3;i++){
//create paragraphs
var para1=document.createElement("p");
//set text
para1.innerHTML="This is para "+(i+1);
//append para to the body
document.body.appendChild(para1);
}
}
//function to generateList()
function generateList()
{
//array
let items = ["my item 1","my item 2","my item 3"];
//create unordered list
var ol=document.createElement("ol");
for(var i=0;i<items.length;i++){
//create listitems
var li=document.createElement("li");
li.innerHTML=items[i];
ol.appendChild(li);
}
//append ol to the body
document.body.appendChild(ol);
}
</script>
</body>
</html>
======================================================
Output : Open web page Exercise1B.html in the browser and will get the screen as shown below
Screen 1 :Exercise1B.html
Screen 2:When para button is clicked
Screen 3:When ol button is clicked
NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.