Question: In a HTML page there are couple of anchor tags with onclick() methods - of which codes I cannot or must not change. However, I can add a new js file or jQuery to this page. How can I add an event listener for click events of those anchor tags - which must be fired/called before those existing onclick() methods? After that the existing onclick() methods should be called. Something like the following.

```
<a href='#' class="edit" onclick='edit()'>Edit First</a>
<a href='#' class="edit" onclick='edit()'>Edit Second</a>
<a href='#' class="edit" onclick='edit()'>Edit Third</a>

```

and

```
const el = document.getElementsByClassName("edit");
el.addEventListener("click", beforeEdit, false);

function beforeEdit() {
 //this should be called before edit(), after that edit() must be called.
}

```

Answer: 
You could use `tcolorbox` for the example:

```
---
header-includes: |
   \BeforeBeginEnvironment{document}{
      \usepackage[most]{tcolorbox}
      \let\example\undefined 
      \let\endexample\undefined 
      \newtcolorbox[auto counter]{example}{
        colback=white,
        colbacktitle=black,
        arc=0mm,
        title={Example~\thetcbcounter:},
        bottom=-.7\baselineskip,
        colframe=black,
        fonttitle=\bfseries
   }}
urlcolor: blue
extension: latex_macros
numbersections: true
output:
   bookdown::pdf_document2:
      toc: false
      keep_tex: true
biblio-style: apalike
---

test

```{example} 
Hello  
```

```

[![enter image description here](https://i.stack.imgur.com/ETp6P.png)](https://i.stack.imgur.com/ETp6P.png)
You can use some packages as for ordinary LaTeX. I give you an example with the packages **thmtools** and **mdframed**.

```
---
output:
  pdf_document: 
    includes:
      in_header: preamble.tex
    keep_tex: yes
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

\begin{example}
    Hello I am an example.
\end{example}

```

File **preamble.tex**:

```
\usepackage{amsthm,thmtools}
\usepackage{mdframed}

\definecolor{verylightgray}{gray}{0.9}

\declaretheoremstyle[
  headfont=\color{black}\normalfont\bfseries,
  bodyfont=\color{black}\normalfont\sffamily,
  mdframed={
    backgroundcolor=verylightgray, 
    hidealllines=true, 
    innertopmargin=2pt, 
    innerbottommargin=2pt, 
    skipabove=\topsep, 
    skipbelow=\topsep
  }]{mystyle} 

\declaretheorem[
  style=mystyle,
  name=Example
]{example}

```

[![enter image description here](https://i.stack.imgur.com/iF55X.png)](https://i.stack.imgur.com/iF55X.png)

A linebreak or a top margin is needed...