js get clicked element code example

Example 1: get id of clicked element javascript

<button id="3" onClick="reply_click(this.id)">B3</button>
    
<script type="text/javascript">
  function reply_click(clicked_id)
  {
      alert(clicked_id);
  }
</script>

Example 2: how to get the text of a clicked elemet by javascript

document.addEventListener('click', function(e) {
    e = e || window.event;
    var target = e.target || e.srcElement,
        text = target.textContent || target.innerText;   
}, false);

Example 3: how to get the text of a clicked elemet by javascript

$(document).click(function(event) {
    var text = $(event.target).text();
});

Tags:

Html Example