Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update rendering dynamically (on button click) #46

Closed
ChenZhongPu opened this issue Sep 9, 2023 · 1 comment
Closed

Update rendering dynamically (on button click) #46

ChenZhongPu opened this issue Sep 9, 2023 · 1 comment
Assignees

Comments

@ChenZhongPu
Copy link

I would like to re-rendering the <pre> in the JS click event. The core logic is to update the content of <pre>, and call pseudocode.renderElement again. But re-rendering fails and it still shows the content before updating.

    const textInput = document.getElementById("input");
    const output = document.getElementById("output");

    const updateButton = document.getElementById("generate");
    updateButton.addEventListener("click", () => {
      const inputValue = textInput.value;
      output.innerHTML = inputValue;
      pseudocode.renderElement(output);
    });
@SaswatPadhi
Copy link
Owner

Hi,

Sorry about the delayed response.

The renderElement function replaces the <pre> element with a new element that contains the rendered output. The rendered output is not simply inserted inside the <pre> element. See:

elem.replaceWith(newElem);

One approach to achieve what you're trying here is to use renderToString# instead. In that case, your #output element could just be a div and you could insert the rendered HTML into it. Something like:

const textInput = document.getElementById("input");
const output = document.getElementById("output");

const updateButton = document.getElementById("generate");
updateButton.addEventListener("click", () => {
  output.innerHTML = pseudocode.renderToString(textInput.value);
});

I tried it out at on https://jsfiddle.net/f7hme2dz/.

# renderToString is fully supported only on KaTeX and on MathJax 3.x backends.

I will mark this issue as resolved and pin it. Please feel free to reopen if you face any further issues.

@SaswatPadhi SaswatPadhi self-assigned this Dec 23, 2023
@SaswatPadhi SaswatPadhi pinned this issue Dec 23, 2023
@SaswatPadhi SaswatPadhi changed the title How to update the rendering in click event? Update rendering dynamically (on button click) Dec 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants