how is going? So I've made this API code here:
index.php
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
// API Endpoint
var x = document.getElementsByClassName("name1");
var y = document.getElementsByClassName("name2");
var i;
var url = 'mysite.org/api.php'
// Fetch the remote file
fetch(url)
.then(function (request) {
// Take the request and parse the data as json
return request.json()
})
.then(function (json) {
// line1
var name1 = json.item1.name // current song title
x.innerHTML = name;
// line2
var name2 = json.item2.name // current song title
y.innerHTML = name2;
});
});
</script>
</head>
<body>
<h5>Hello</h5>
<div class="name1"></div>
<div class="name2"></div>
</body>
</html>
api.php ((the json is generated by array from a cicle while))
{"item1":{"id":"1","name":"Daniel"},"item2":{"id":"2","name":"Kalifa"}}
So this code works fine and I can see item1 inside div id name1 and item2 inside div id name2. But I dunno why but when I try to change the elements from ID to CLASS it doesn't work. Any ideas?