title: document.addEventListener 'copy'.. working, but element.addEventListener 'copy' .. does not work
body: <p>I am using PrimeFaces 7.0 and want to enable a Java Script eventListener for the "copy" event, as described here:</p>
  
  <p><a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/copy_event" rel="nofollow noreferrer">https://developer.mozilla.org/en-US/docs/Web/API/Element/copy_event</a></p>
  
  <p>When I attach the eventListener to the document, everything works fine:</p>
  
  <p>My Java Script function:</p>
  
  <pre><code>function jsCopyAction(){
      document.addEventListener('copy', (event) =&gt; {
          const selection = document.getSelection();
          event.clipboardData.setData('text/plain', selection.toString());
          event.preventDefault();
          document.getElementById("badTransFormId:jsCopyTextId").value=selection.toString();
  
          var jsfCommandButton = document.getElementById("badTransFormId:jsCopyLinkId");
          jsfCommandButton.click();
      });
  };
  </code></pre>
  
  <p>My facelet page:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui"&gt;
      &lt;h:head&gt;
      &lt;/h:head&gt;
      &lt;h:body&gt;
          &lt;h:outputStylesheet name="primeicons/primeicons.css" library="primefaces"/&gt;
          &lt;h:outputStylesheet library="css" name="styles.css"  /&gt;
          &lt;h:outputScript library="primefaces" name="jquery/jquery.js"/&gt;
          &lt;h:outputScript library="js" name="commons.js" /&gt;
          &lt;script&gt;
                  // we need to execute the js init() function after the page load:
                  // @see https://stackoverflow.com/questions/8996316/how-to-execute-javascript-after-page-load
                  jQuery(document).ready(function() {
                      jQuery(document).ready(function() {
                          // twice in document.ready to execute after Primefaces callbacks
                          jsCopyAction();
                      });
                  });
          &lt;/script&gt;
          &lt;h:form id="badTransFormId"&gt;
              &lt;div&gt;
                  &lt;div&gt;
                      &lt;h:panelGrid columns="2" style="margin-bottom:10px" cellpadding="5" columnClasses="label, value"&gt;
                          &lt;h:commandButton style="display:none" id="jsCopyLinkId" action="#{badTransBean2.jsCopyAction}"&gt;
                              &lt;f:ajax execute = "badTransFormId:jsCopyTextId" render = "@form"/&gt;
                          &lt;/h:commandButton&gt;
                          &lt;input type="hidden" id="badTransFormId:jsCopyTextId" name = "jsCopyText" value="" /&gt;
                      &lt;/h:panelGrid&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div id="translEntityDiv"&gt;
                &lt;h:outputText id= "correspHomeEntityId" value="Alabala alabala tralala" /&gt;
              &lt;/div&gt;
          &lt;/h:form&gt;
      &lt;/h:body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>My Bean:</p>
  
  <pre><code>@ManagedBean(name = "badTransBean2")
  @ViewScoped
  public class BadTransBean2 implements Serializable {
  
  
      public void jsCopyAction() {
          HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext()
                  .getRequest();
          String jsCopyText = req.getParameter("jsCopyText");
  
          if (StringUtils.isEmpty(jsCopyText) || StringUtils.isEmpty(jsCopyText.trim())) {
              return;
          }
          jsCopyText = jsCopyText.trim().toLowerCase();
  
          System.out.println(jsCopyText);
      }
  
  }
  </code></pre>
  
  <p>However, when I want to be more specific and want to attach the EventListener to a particular HTML element, this works ONLY the first time the user copies an element after the page is loaded in the browser. When the user copies an element, the Java Script event listener is not fired at all. Tested on Chrome and Mozilla.</p>
  
  <p>My changed JavaScript function:</p>
  
  <pre><code>function jsCopyAction(){
  
      const source = document.getElementById("badTransFormId:correspHomeEntityId");
  
      source.addEventListener('copy', (event) =&gt; {
          const selection = document.getSelection();
          event.clipboardData.setData('text/plain', selection.toString());
          event.preventDefault();
          document.getElementById("badTransFormId:jsCopyTextId").value=selection.toString();
  
          var jsfCommandButton = document.getElementById("badTransFormId:jsCopyLinkId");
          jsfCommandButton.click();
      });
  };
  </code></pre>
  
  <p>My pom.xml:</p>
  
  <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
  &lt;groupId&gt;seller&lt;/groupId&gt;
  &lt;artifactId&gt;home.digest&lt;/artifactId&gt;
  &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  &lt;packaging&gt;war&lt;/packaging&gt;
  
  &lt;name&gt;home.digest Maven Webapp&lt;/name&gt;
  &lt;!-- FIXME change it to the project's website --&gt;
  &lt;url&gt;http://maven.apache.org&lt;/url&gt;
  &lt;repositories&gt;
      &lt;repository&gt;
          &lt;id&gt;prime-repo&lt;/id&gt;
          &lt;name&gt;Prime Repo&lt;/name&gt;
          &lt;url&gt;http://repository.primefaces.org&lt;/url&gt;
      &lt;/repository&gt;
  &lt;/repositories&gt;
  
  &lt;properties&gt;
      &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
      &lt;maven.compiler.source&gt;1.8&lt;/maven.compiler.source&gt;
      &lt;maven.compiler.target&gt;1.8&lt;/maven.compiler.target&gt;
  &lt;/properties&gt;
  
  &lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;junit&lt;/groupId&gt;
          &lt;artifactId&gt;junit&lt;/artifactId&gt;
          &lt;version&gt;4.11&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.seleniumhq.selenium&lt;/groupId&gt;
          &lt;artifactId&gt;selenium-java&lt;/artifactId&gt;
          &lt;version&gt;3.141.59&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.apache.httpcomponents&lt;/groupId&gt;
          &lt;artifactId&gt;httpcore&lt;/artifactId&gt;
          &lt;version&gt;4.4.10&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.apache.httpcomponents&lt;/groupId&gt;
          &lt;artifactId&gt;httpclient&lt;/artifactId&gt;
          &lt;version&gt;4.5.6&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.primefaces&lt;/groupId&gt;
          &lt;artifactId&gt;primefaces&lt;/artifactId&gt;
          &lt;version&gt;7.0&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.jboss.logging&lt;/groupId&gt;
          &lt;artifactId&gt;jboss-logging&lt;/artifactId&gt;
          &lt;version&gt;3.0.1.GA&lt;/version&gt;
          &lt;scope&gt;compile&lt;/scope&gt;
          &lt;optional&gt;true&lt;/optional&gt;
      &lt;/dependency&gt;
      &lt;!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.1-api --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate.javax.persistence&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-jpa-2.1-api&lt;/artifactId&gt;
          &lt;version&gt;1.0.2.Final&lt;/version&gt;
          &lt;scope&gt;provided&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
          &lt;version&gt;4.3.6.Final&lt;/version&gt;
           &lt;scope&gt;provided&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;!-- https://mvnrepository.com/artifact/javax.enterprise/cdi-api --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;javax.enterprise&lt;/groupId&gt;
          &lt;artifactId&gt;cdi-api&lt;/artifactId&gt;
          &lt;version&gt;2.0.SP1&lt;/version&gt;
          &lt;scope&gt;provided&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.jboss.as&lt;/groupId&gt;
          &lt;artifactId&gt;jboss-as-web&lt;/artifactId&gt;
          &lt;version&gt;7.1.1.Final&lt;/version&gt;
          &lt;scope&gt;compile&lt;/scope&gt;
          &lt;optional&gt;true&lt;/optional&gt;
      &lt;/dependency&gt;
      &lt;!-- https://mvnrepository.com/artifact/org.jboss.spec.javax.ejb/jboss-ejb-api_3.2_spec --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.jboss.spec.javax.ejb&lt;/groupId&gt;
          &lt;artifactId&gt;jboss-ejb-api_3.2_spec&lt;/artifactId&gt;
          &lt;version&gt;1.0.2.Final&lt;/version&gt;
          &lt;scope&gt;provided&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;!-- https://mvnrepository.com/artifact/com.google.cloud/google-cloud-translate --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.google.cloud&lt;/groupId&gt;
          &lt;artifactId&gt;google-cloud-translate&lt;/artifactId&gt;
          &lt;version&gt;1.79.0&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;!-- jsoup HTML parser library @ https://jsoup.org/ --&gt;
          &lt;groupId&gt;org.jsoup&lt;/groupId&gt;
          &lt;artifactId&gt;jsoup&lt;/artifactId&gt;
          &lt;version&gt;1.11.3&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
          &lt;artifactId&gt;commons-lang3&lt;/artifactId&gt;
          &lt;version&gt;3.8.1&lt;/version&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  
  &lt;build&gt;
      &lt;finalName&gt;home.digest&lt;/finalName&gt;
      &lt;pluginManagement&gt;&lt;!-- lock down plugins versions to avoid using Maven 
              defaults (may be moved to parent pom) --&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-clean-plugin&lt;/artifactId&gt;
                  &lt;version&gt;3.1.0&lt;/version&gt;
              &lt;/plugin&gt;
              &lt;!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --&gt;
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-resources-plugin&lt;/artifactId&gt;
                  &lt;version&gt;3.0.2&lt;/version&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                  &lt;version&gt;3.8.0&lt;/version&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.22.1&lt;/version&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
                  &lt;version&gt;3.2.2&lt;/version&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-install-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.5.2&lt;/version&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-deploy-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.8.2&lt;/version&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/pluginManagement&gt;
  &lt;/build&gt;
  </code></pre>
  
  <p></p>
  
  <p>What might be the reason?</p>