- I prefer not to rely on jQuery for this task.
- If possible, I would like to maintain the presence of
<mytag>
.
The JSON data structure I am working with is as follows:
{
"datas": [
{
"id": "hotel_name",
"value": "My hotel name"
},
{
"id": "hotel_description",
"value": "My description"
}
]
}
Based on this JSON, my objective is to populate the following html tags with the respective content:
<mytag id="hotel_name"></mytag>
<mytag id="hotel_description"></mytag>
Resulting in:
<mytag id="hotel_name">My hotel name</mytag>
<mytag id="hotel_description">My description</mytag>
Here is what I have attempted so far:
function createElements(elements) {
elements.datas.forEach(function (element) {
var div = document.getElementById(element.id);
div.innerHTML = element.value;
});
}
var request = new XMLHttpRequest();
request.onload = createElements;
request.responseType = "json";
request.open("GET", "datas.json", true);
request.send();