Feedback Details HTML & JavaScript Program
Get the feedback details from the user and add it to an array. Display the feedback details in the format as shown in the screenshot.
Concept covered: functions, arrays, control structures and loops, String
Problem Specification:
- File name should be index.html.
- Create an external script file named script.js.
- Use h2 tag for the title of Feedback Details.
- Display all the feedbacks followed by a new line separator after printing the above title.
- Refer Sample screenshot for more specifications.
- Include the script file in html page.
- The “Add Feedback” button will add the feedback details to an array.
- The “View Feedback” button will display all the feedbacks (multiple feedbacks) in the format as shown in the screenshot in the result div.
- Include the below functions/methods in the java script file
Function Name | Description |
addFeedback() | It is used to add the feedback details to an array. After clicking the Add Feedback button, clear the text area and display the response message as “Successfully Added Feedback Details!”. |
displayFeedback() | It is used to display the feedback details. After displaying the feedback details clear the array. |
index.html
<html> <head> <script src="script.js" type="text/javascript"> </script> </head> <body> <!-- Fill code here --> <h1>Feedback for the ART OF LIVING SESSION</h1> Enter the Feedback:<textarea id="feedback"></textarea> <button id="create" onclick="addFeedback()">Add Feedback</button> <button id="view" onclick="displayFeedback()">View Feedback</button> <div id="result"></div> </body> </html>
Script.js
var feedbacks=[]; function addFeedback(){ //Fill the required logic const newFeedback=document.getElementById("feedback").value; feedbacks.push(newFeedback); console.log(feedbacks); document.getElementById("feedback").value=""; document.getElementById("result").innerHTML="<h2>Feedback Details</h2><b>Successfully Added Feedback Details!</b>"; } function displayFeedback(){ //Fill the required logic document.getElementById("result").innerHTML="<h2>Feedback Details</h2>"; for (var i = 0; i < feedbacks.length ; i++) { document.getElementById("result").innerHTML += "Feedback "+(i+1)+"<br>"+feedbacks[i]+"<br>"; } feedbacks=[]; }