JavaScript Simple Actions
In JavaScript, simple actions are typically straightforward tasks such as manipulating elements, responding to events, or performing calculations. These actions form the core functionality for interactivity and behavior on websites. Below are some examples of common and simple JavaScript actions:
1️⃣ Changing Text Content
You can change the text content of an HTML element using the innerText
or textContent
property:
2️⃣ Changing Style (CSS) Dynamically
You can change the style of an element using JavaScript by modifying its style
property:
This changes the background color of the page when the button is clicked.
3️⃣ Alert Message
You can display a simple alert message using the alert()
function:
This shows a pop-up alert when the button is clicked.
4️⃣ Logging to Console
You can output information to the browser's console using console.log()
:
This logs a message to the browser console every time the button is clicked.
5️⃣ Prompting for User Input
You can ask the user for input using the prompt()
function:
The user is prompted for their name, and an alert greeting them is shown.
6️⃣ Hiding and Showing Elements
You can hide or show elements dynamically by changing their style.display
property:
This toggles the visibility of the paragraph when the button is clicked.
7️⃣ Changing HTML Content
You can change the entire HTML content of an element with innerHTML
:
This changes the content of the div
element when the button is clicked.
8️⃣ Performing Simple Calculations
You can perform simple arithmetic calculations directly in JavaScript:
This calculates the sum of two numbers and displays the result in an alert.
9️⃣ Manipulating Arrays
You can perform simple actions on arrays, such as adding, removing, or modifying elements:
This adds an item to the fruits
array and logs the updated array.
🔟 Setting a Timer
You can use setTimeout()
to execute a function after a specified delay or setInterval()
to execute a function repeatedly at a given interval.
setTimeout()
setInterval()
1️⃣1️⃣ Changing an Element’s Attributes
You can modify an element's attributes, such as src
, href
, id
, etc.
This changes the src
attribute of an image when the button is clicked.
1️⃣2️⃣ Animating an Element
You can animate elements using setInterval()
for basic animations:
This moves a div
element horizontally across the page.
Conclusion
JavaScript is incredibly versatile, and these simple actions can be used to create dynamic and interactive experiences for users on the web. By combining event handling, DOM manipulation, and basic functionality, you can build much more complex and responsive applications.
Let me know if you'd like to dive deeper into any of these actions! 😊