Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • M MathJax
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 304
    • Issues 304
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 15
    • Merge requests 15
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • MathJax
  • MathJax
  • Issues
  • #2926
Closed
Open
Issue created Sep 20, 2022 by Scruel Tao@scruel

How to add event listeners to rendered chtml?

I want to dynamically render math blocks, and convert content to chtml: The following code works fine:

document.body.append(MathJax.tex2chtml('\\sqrt{x^2+1}', {em: 12, ex: 6, display: false})); 

But what if I can only use it's content, rather than directly add it to the page?

// Render HTML
function before(content) {
  const equation = ...;
  const equationHTML = MathJax.tex2chtml(equation, {em: 12, ex: 6, display: false}).outerHTML;
  html = replace(equation, equationHTML)
  // only can return string, rather than DOM elements
  return html;
}

function after(html) {
  // ...
}

For both Mathjax V2 and V3, I wonder after I appended chtml into specific DOM element, how can I add listeners (like right-click context menu)? Since there are no method called getEventListeners, so I can't do any restore behaviors just like innerHTML.

Currently, I have to do something like:

// Render HTML
function before(content) {
  const equation = ...;
  // Need to escape equations to prevent default behaviours when convert content to hmtl
  const equationHTML = '<div id='equation'>escape(equation)</div>';
  html = replace(equation, equationHTML)
  // only can return string, rather than DOM elements
  return html;
}

function after(html) {
  // ...
}

function done() {
  MathJax.typesetPromise([docuement.querySelector('#equation')]);
}

To make sure that elements will have listeners, because the element rendered by MathJax directly.

Assignee
Assign to
Time tracking