-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
37 lines (30 loc) · 1.37 KB
/
index.js
File metadata and controls
37 lines (30 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Define API URL
const apiUrl = 'https://swapi.dev/api/people';
// Fetch character data and populate character list
async function fetchCharacters() {
const response = await fetch(apiUrl);
const data = await response.json();
const characterList = document.getElementById('character-list');
data.results.forEach(character => {
const listItem = document.createElement('li');
listItem.className = 'list-group-item';
listItem.textContent = character.name;
listItem.addEventListener('click', () => displayCharacterDetails(character));
characterList.appendChild(listItem)
});
}
// Display character details
function displayCharacterDetails(character) {
const characterImage = document.getElementById('character-image');
const characterName = document.getElementById('character-name');
const characterGender = document.getElementById('character-gender');
const characterHeight = document.getElementById('character-height');
const characterDetails = document.getElementById('character-details');
characterImage.src = './image.avif'//'path_to_character_image.jpg'; // Replace with actual image path
characterName.textContent = character.name;
characterGender.textContent = character.gender;
characterHeight.textContent = character.height;
characterDetails.style.display = 'block';
}
// Initial function to start fetching data
fetchCharacters();