Draw a flowchart that shows how JavaScript adds interactivity to a webpage by responding to a button click event. The flowchart should start with the user clicking a button, then show JavaScript detecting the click, running a function to change the text on the page, and finally ending when the text is updated.
10 minutes
Hint 1
Hint 2
Hint 3
Hint 4
Hint 5
Grading Criteria
Start and End oval symbols present
User click event represented as input (parallelogram)
JavaScript detecting event shown as a process (rectangle)
Function running to change text shown as a process (rectangle)
Text update shown as output (parallelogram)
Flow direction is clear and logical from start to end
Solution
_______
/ \
| Start |
\_______/
|
v
________________
/ User clicks \
| button (event) |
\________________/
|
v
____________________
/ JavaScript detects \
| the click event |
\__________________/
|
v
____________________
/ Run function to \
| change text on page |
\__________________/
|
v
____________________
/ Text on page is \
| updated |
\__________________/
|
v
_______
/ \
| End |
\_______/
This flowchart starts with the user clicking a button on the webpage, which is the input event. JavaScript detects this click event and then runs a function. The function changes the text on the page, which is the output. Finally, the flowchart ends after the text is updated. Each step is represented by standard flowchart symbols: ovals for start/end, parallelograms for input/output, and rectangles for processing steps.
Variations - 2 Challenges
[intermediate] Draw a flowchart showing how JavaScript responds to a user entering text in a textbox and then clicking a submit button to display a greeting message.
[advanced] Draw a flowchart for JavaScript interactivity where a user clicks a button to toggle the visibility of an image on the webpage (show/hide).
Practice
(1/5)
1. What is the main purpose of JavaScript on a webpage?
easy
A. To style the webpage with colors and fonts
B. To make the webpage interactive by responding to user actions
C. To store data permanently on the server
D. To create static text content only
Solution
Step 1: Understand JavaScript's role
JavaScript is used to add interactivity, meaning it reacts to what users do on the page.
Step 2: Compare with other web technologies
Styling is done by CSS, and static content is HTML. Data storage is server-side, not JavaScript's main job.
Final Answer:
To make the webpage interactive by responding to user actions -> Option B
Quick Check:
JavaScript = Interactivity [OK]
Hint: JavaScript = webpage actions and reactions [OK]
Common Mistakes:
Confusing JavaScript with CSS for styling
Thinking JavaScript stores data permanently
Believing JavaScript only creates static content
2. Which of the following is the correct way to add a click event listener to a button with id myBtn in JavaScript?
easy
A. document.querySelector('myBtn').addEventListener('click', alert('Clicked!'));
B. document.getElementById('myBtn').onClick = alert('Clicked!');
C. document.getElementById('myBtn').addEventListener('click', function() { alert('Clicked!'); });
D. document.getElementById('myBtn').addEventListener('onclick', function() { alert('Clicked!'); });
Solution
Step 1: Identify correct method and event name
The method to add event listeners is addEventListener and the event name is 'click', not 'onclick'.
Step 2: Check function syntax
The event handler should be a function, so function() { alert('Clicked!'); } is correct. Directly calling alert('Clicked!') passes the result, not the function.
Final Answer:
document.getElementById('myBtn').addEventListener('click', function() { alert('Clicked!'); }); -> Option C
Quick Check:
Use addEventListener('click', function) [OK]
Hint: Use addEventListener with event name and function [OK]
Common Mistakes:
Using 'onclick' instead of 'click' in addEventListener
Assigning event handler directly instead of a function
Using querySelector without '#' for id
3. What will be the output when the following code runs and the button is clicked?
A. The getElementById method is incorrect and should be querySelector
B. The event listener should use 'onclick' instead of 'click'
C. The function changeColor should be anonymous inside addEventListener
D. The property 'background' should be 'backgroundColor' to change color
Solution
Step 1: Check CSS property used in JavaScript
To change background color, the correct style property is backgroundColor, not background.
Step 2: Verify event listener and function usage
Using 'click' event and named function is valid. getElementById is correct for id selection.
Final Answer:
The property 'background' should be 'backgroundColor' to change color -> Option D
Quick Check:
Use style.backgroundColor to change background color [OK]
Hint: Use style.backgroundColor, not style.background [OK]
Common Mistakes:
Using 'background' instead of 'backgroundColor'
Confusing 'click' event with 'onclick'
Thinking function must be anonymous
5. You want to create a button that toggles the visibility of a paragraph with id text each time it is clicked. Which code snippet correctly implements this behavior?
hard
A. const btn = document.getElementById('btn');
const text = document.getElementById('text');
btn.addEventListener('click', () => {
if (text.style.display === 'none') {
text.style.display = 'block';
} else {
text.style.display = 'none';
}
});
B. const btn = document.getElementById('btn');
const text = document.getElementById('text');
btn.addEventListener('click', () => {
text.style.visibility = 'hidden';
});
C. const btn = document.getElementById('btn');
const text = document.getElementById('text');
btn.onclick = function() {
text.style.display = 'visible';
};
D. const btn = document.getElementById('btn');
const text = document.getElementById('text');
btn.addEventListener('click', () => {
text.style.display = 'block';
});
Solution
Step 1: Understand toggle logic
To toggle visibility, check if display is 'none' and switch it to 'block', else set to 'none'.
Step 2: Analyze each option
const btn = document.getElementById('btn');
const text = document.getElementById('text');
btn.addEventListener('click', () => {
if (text.style.display === 'none') {
text.style.display = 'block';
} else {
text.style.display = 'none';
}
}); correctly toggles between 'none' and 'block'. const btn = document.getElementById('btn');
const text = document.getElementById('text');
btn.addEventListener('click', () => {
text.style.visibility = 'hidden';
}); only hides once. const btn = document.getElementById('btn');
const text = document.getElementById('text');
btn.onclick = function() {
text.style.display = 'visible';
}; uses invalid 'visible' for display. const btn = document.getElementById('btn');
const text = document.getElementById('text');
btn.addEventListener('click', () => {
text.style.display = 'block';
}); only shows without toggling.