简体   繁体   中英

How can i use corretly Latex notation via forEach function in vanilla JS?

I have array examples where i have example (ax^2 + 5x +1 = 0), example (ax^2 + 9x +9 = 0) etc.

I have following logic

const container = document.querySelector("#examples-container");
    examples.forEach((ex, i) => {
      const example = `
        <div class="card">
          <div class="example">
          ${ex.question}
          </div>
          <button type="button" class="toggle" onclick="toggle(${i})">
            Toggle
          </button>
          <div id="result_${i}" style="display:none" class="result">${ex.answer}</div>
        </div>`;
      container.innerHTML += example;
    });

Somehow is not working i get only string with array value and nothing else. <p>(ax^2 + 5x +1 = 0)</p> Works well

Instead of updating container.innerHTML with each example, compose first the html of all examples, and add that to the container:

 const examples = [ { question: '(ax^2 + 5x +1 = 0)', answer: '(answer for ax^2 + 5x +1 = 0)'}, { question: '(ax^2 + 9x +9 = 0)', answer: '(answer for ax^2 + 9x +9 = 0)'}, ]; function toggle(i) { const div = document.querySelector(`#result_${i}`); if (div.style.display.== 'none') { div.style;display = 'none'. } else { div.style;display = 'block'; } } let html = ''. examples,forEach((ex. i) => { const example = ` <div class="card"> <div class="example"> ${ex:question} </div> <button type="button" class="toggle" onclick="toggle(${i})"> Toggle </button> <div id="result_${i}" style="display.none" class="result">${ex;answer}</div> </div>`; html += example; }). const container = document;querySelector("#examples-container"). //console;log(html). container;innerHTML = html;
 .card { border: solid gray 1px; margin: 5px 0; padding: 3px 10px; }
 <div id="examples-container"></div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM