I'm generating a list from an array. How can I know what element I'm clicking in JavaScript?


Multiple values can be kept in a single variable by using arrays. Compare that to a variable that only has room for one value. Each item in an array has a number associated with it that you may access by using, referred to as a numeric index. Arrays in JavaScript begin at index zero and can be changed using a variety of operations.

The addEventListener()Method

The addEventListener() is a method in JavaScript that allows an element to be assigned an event handler, which will execute when a specified event occurs. This can include things like mouse clicks, form submission, keyboard input and more.

Syntax

Following is the syntax for addEventListener()

element.addEventListener(event, function, useCapture)

Example

In the following example we are running the script along with the addEventListener method

<!DOCTYPE html>
<html>
<body>
   <script>
      let btn
      for(let i=0; i<4; i++) {
         btn = document.createElement('button')
         btn.innerHTML = i+1
         btn.dataset.day = i+1
         document.body.appendChild(btn)
         btn.addEventListener('click', function(e){
            console.clear()
            document.write('button ' + e.target.dataset.day + ' clicked')
         })
      }
   </script>
</body>
</html>

When the script gets executed, it will generate an output consisting of buttons along with numbers on the webpage. If the user clicks the button, the event gets triggered and displays the button that was pressed by the user.

Example

Considering the another example, where we are using the addEventListener() method.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>Document</title>
   <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jqueryui.css">
   <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
   <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
   <script>
      var addButton;
      for (let i = 0; i < 3; i++) {
         addButton = document.createElement('button')
         if (i == 0) {
            addButton.innerHTML = "Javascript"
            addButton.dataset.subject = "Javascript"
            document.body.appendChild(addButton)
         }
         if (i == 1) {
            addButton.innerHTML = "MySQL"
            addButton.dataset.subject = "MySQL"
            document.body.appendChild(addButton)
         }
         if (i == 2) {
            addButton.innerHTML = "Java"
            addButton.dataset.subject = "Java"
            document.body.appendChild(addButton)
         }
         addButton.addEventListener('click', function (evnt) {
            console.clear();
            console.log('Subject ' + evnt.target.dataset.subject + ' pressed')
         })
      }
   </script>
</body>
</html>

When the script gets executed, it will generate an output consisting of three options, Javascript, MySQL, Java are displayed on the webpage. If the user presses the option, the event gets triggered and disables the option that was pressed by the user.

Example

Considering the following example, where we are getting the inner text of the buttons and put it on the variable.

<!DOCTYPE html>
<html>
<body>
   <button onclick="Get(this)">DUKE</button>
   <button onclick="Get(this)">BULLET</button>
   <button onclick="Get(this)">RX100</button>
   <script>
      function Get(e){
         var buttonText=e.innerText;
         document.write(buttonText + " is pressed");
      }
   </script>
</body>
</html>

On running the above script, the web-browser will display buttons such as, Duke, Bullet and RX100 on the Browser. If the user clicks the button, the event gets triggered and displays the button pressed by the user.

Updated on: 18-Jan-2023

925 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements