Answer the question
In order to leave comments, you need to log in
How to set innerHTML element correctly?
I have such a problem: I receive data from the server using an Http request. From there, the data needs to be inserted into the template, I do it in js, but it turns out something like this:
const questionWrapper = document.createElement('div');
questionWrapper.innerHTML = `<div class="single-question">
<div class="single-question__action-info">
<span class="single-question__action-id">
Акция №${el.event_id}
</span>
</div>
<div class="single-question__content">
<div class="single-question__question">
<h5 class="single-question__question-title">Вопрос <span class="open-input open-input_question"><img src="img/question-icon.svg" alt=""></span></h5>
<div class="single-question__question-text">
${el.question}
</div>
<div class="input-wrapper">
<textarea class="question-textarea" name="" id=""></textarea>
<button class="submit-textarea submit-textarea_question">Ответить</button>
</div>
<div class="single-question__answer">
<h5 class="single-question__answer-title">Ответ <span class="open-input open-input_answer open-input_grey"><img src="img/question-icon.svg" alt=""></span></h5>
<div class="single-question__answer-text">
${el.answer}
</div>
<div class="input-wrapper">
<textarea class="answer-textarea" name="" id=""></textarea>
<button class="submit-textarea submit-textarea_answer">Ответить</button>
</div>
</div>`;
$('.answers__list').insertBefore(questionWrapper, $('.single-question_wrapper'));
Answer the question
In order to leave comments, you need to log in
As for the template engine, you can write a very simple one of your own.
var html = `
<div class="single-question">
<span class="{{class}}">{{text}}</span>
</div>
`;
var result = tag(html, {
class: 'test',
text: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eaque, magni.'
});
function tag(input, data) {
for (var tag in data) {
input = input.replace(new RegExp('\\{{'+ tag +'\\}}', 'gi'), data[tag]);
}
return input;
}
<div class="single-question">
<span class="test">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eaque, magni.</span>
</div>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question