Events in javaScript

Events :

an event is an action or occurrence that happens in the browser that can be detected and responded to by the code. Events are an essential part of web development as they enable interactive user interfaces and allow developers to build applications that respond to user actions.

Events can be triggered by various actions such as a user clicking a button, a page finishing loading, or a user typing in a form field. JavaScript provides a way to capture these events and respond to them using event listeners.

Event listeners are functions that are executed when a specific event occurs. They are attached to HTML elements and listen for events on those elements. Event listeners can be added using the addEventListener() method.

For example, the following code adds an event listener to a button element that listens for the click event and executes a function when the button is clicked:

const button = document.querySelector(‘button’);

button.addEventListener(‘click’, function()


console.log(‘Button clicked!’);


JavaScript provides a wide range of events that can be detected and responded to. Some of the most common events include:

  • click: Triggered when an element is clicked.
  • submit: Triggered when a form is submitted.
  • keydown, keyup: Triggered when a key is pressed or released on the keyboard.
  • load: Triggered when a page finishes loading.
  • resize: Triggered when the browser window is resized.

Events can be very useful for building dynamic and interactive web applications. They allow developers to create rich user interfaces that respond to user actions, and provide a way to make web pages more engaging and interactive.

click here to go back

AJAX and APIs :


AJAX (Asynchronous JavaScript and XML) is a technique used in JavaScript to send and receive data from a web server asynchronously without the need to refresh the entire web page. The technique is based on using the XMLHttpRequest object to make asynchronous HTTP requests to a server and receiving responses in various formats such as JSON, XML, or plain text. Here are the basic steps involved in using AJAX in JavaScript:

a. Create an instance of the XMLHttpRequest object

const xhttp = new XMLHttpRequest();

b. Define a callback function to handle the server response when it arrives. This function is called whenever the state of the XMLHttpRequest changes, and it typically checks whether the request is complete (readyState=4) and whether the server response was successful (status=200).

xhttp.onreadystatechange = function() {

if (this.readyState == 4 && this.status == 200) {

// Handle the server response here



c. Open a connection to the server and specify the HTTP method (e.g., GET or POST), the URL of the server endpoint, and whether the request should be asynchronous (true) or synchronous (false).“GET”, “”, true);

d. Send the request to the server. If the request is a POST request, you can specify the data to send in the send() method. For a GET request, the data can be included in the URL.


e. Inside the callback function, you can handle the server response by accessing the responseText or responseXML properties of the XMLHttpRequest object. The responseText property contains the server response as a string, while the responseXML property contains the server response as an XML document (if the response is in XML format).

if (this.readyState == 4 && this.status == 200) {

const response = JSON.parse(this.responseText);

console.log(response); }

You can also use other HTTP client libraries such as Fetch or Axios to make AJAX requests in JavaScript. The basic principles are the same, but the syntax and features may differ.

click here to go back

2. APIs

APIs (Application Programming Interfaces) are interfaces that allow software applications to communicate with each other and exchange data or services. In the context of JavaScript, APIs typically refer to web APIs that are accessible over the internet and provide various services such as retrieving data, performing calculations, or integrating with other services. Here are the basic steps involved in using an API in JavaScript:

a. Find a suitable API endpoint and familiarize yourself with its documentation. The documentation typically includes information about the available endpoints, request and response formats, authentication requirements, and rate limits.

b. Decide which HTTP client library to use. JavaScript provides several libraries for making HTTP requests, such as XMLHttpRequest, Fetch, and Axios. Choose the library that best suits your needs and preferences.

c. Make a request to the API endpoint using the chosen library. The request typically includes the HTTP method (e.g., GET or POST), the URL of the API endpoint, and any required parameters or headers.

Here is an example of making a request to the OpenWeatherMap API using the Fetch API:

const apiKey = “YOUR_API_KEY”;

const url = `${apiKey}`;


.then(response => response.json())

.then(data => console.log(data))

.catch(error => console.error(error));

In this example, we first define the API key for the OpenWeatherMap API, then construct the URL of the API endpoint with the desired query parameters (in this case, the city of London). We then use the Fetch API to make a GET request to the API endpoint and handle the response using the json() method to parse the response as JSON.

d. Process the response from the API. Depending on the format of the response, you may need to parse it as JSON, XML, or some other format. You can then extract the relevant data from the response and use it in your application as needed.

Here is an example of processing the response from the OpenWeatherMap API and displaying the current temperature on a web page:

const apiKey = “YOUR_API_KEY”;

const url = `${apiKey}`;


.then(response => response.json())

.then(data => {

const temperature = data.main.temp;

document.getElementById(“temperature”).innerHTML = `${temperature} K`;


.catch(error => console.error(error));

In this example, we extract the current temperature from the response data and update the innerHTML of an HTML element with the ID “temperature” to display the temperature in Kelvin.

click here to go back