JavaScript is an incredibly versatile programming language, often used to make web pages interactive and provide online programs, including video games. Here are a few examples of how it can be used.
Form Validation
In this script, an event listener is added to the form with the ID myForm. If the form is submitted and either the username or password field is empty, an alert will be shown and the form won't be submitted.
// obtain an element by its "id" attribute and assign it to a variable
let form = document.getElementById('myForm');
// tell the form that when it is submitted execute some specific actions
form.addEventListener('submit', function(event) {
let username = document.getElementById('username');
let password = document.getElementById('password');
// in case username or password field value are empty
if(username.value == '' || password.value == '') {
// display a browser message
alert('Both fields must be filled out!');
// prevent the browser from performing its default action when the submit event occurs
event.preventDefault();
}
});
The rest of the content (3 read minutes) is restricted.
Please use your personal access token or register to access.