Here I'm trying to parse the JSON data through the search button. After searching an example "iPhone" it shows 15 results and 15 buttons through the loop. Now on the buttons I have made onclick attribute. But it's giving the mentioned error. I did this the same way before on another project. It worked. But now It's not working! My HTML code is here-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<link rel="stylesheet" href="css/style.css">
<link rel="shortcut icon" href="images/gadget.png" type="image/x-icon">
<script src="https://kit.fontawesome.com/d7e626f44b.js" crossorigin="anonymous"></script>
<title>Goriber Gadget</title>
</head>
<body>
<!-- header -->
<section class="d-flex justify-content-center align-items-center">
<h1>Goriber Gadget</h1>
<i class="fa-solid fa-phone d-block fs-3"></i>
</section>
<!-- header end -->
<!-- input -->
<section class="container mx-auto d-flex justify-content-center">
<input id="input" type="text" placeholder="Search your phone" class="w-75">
<button id="search" class="btn btn-primary">Submit</button>
</section>
<!-- input end -->
<!-- detail section -->
<section id="detail-section">
</section>
<!-- card -->
<div id="card-section" class="row row-cols-1 row-cols-md-3 g-4 container mx-auto">
</div>
<!-- card end -->
<script src="js/app.js"></script>
</body>
</html>
My JS code is here:
const inputField= document.getElementById('input');
const searchButton= document.getElementById('search');
const cardSection= document.getElementById('card-section');
searchButton.addEventListener('click',function(event){
const inputValue= inputField.value;
fetch(`https://openapi.programming-hero.com/api/phones?search=${inputValue}`)
.then(Response=>Response.json())
.then(data=>display(data))
})
const display= data=>{
if(data.data.length>0){
for(const myData of data.data){
const div= document.createElement('div');
div.innerHTML=`<button onclick='moreDetail(${myData.slug})'>Submit</button>`
div.classList.add('col');
cardSection.appendChild(div);
}
} else{
cardSection.innerHTML= `
<div class="not-found"><i class="fa-solid d-inline-block fa-exclamation"></i><i class="fa-solid d-inline-block fa-heart-crack"></i>
<p>No Result Found</p></div>
`
}
}
const detailSection= document.getElementById('detail-section');
function moreDetail(id){
console.log(id);
// fetch(`https://openapi.programming-hero.com/api/phone/${id}`)
// .then(Response=>Response.json())
// .then(data=>displayMore(data))
}