I am trying to create a element in JavaScript and inserting it into the dom.
let div = document.createElement('div');
div.innerHTML = '<p id="' + selected.val() + '">' + selected.text() + '</p>';
document.getElementById('pickedLingua').appendChild(div);
Working fine, but the problem is, that when I try to stylize the element, I think this html string will become very hard to read.
Can I maybe create a html template and insert the data into it? There must be a better way then to just create a long html string that no one can read after.
2
Answers
Yes, you can create an HTML template and insert the data into it using JavaScript. Here is an example:
HTML:
JavaScript:
In this example, we’re using the querySelector method to find the #lingua element inside the template instance and modify its id and textContent properties. This will make your code more readable and easier to maintain
To make it easier to read, you could make the HTML string using backticks.