OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How can I change the css with js while fetching data?

  • Thread starter Thread starter Ayli
  • Start date Start date
A

Ayli

Guest
I want to fetch data but I want it to show less data each time the screen gets smaller. How can I do that?

I tried this, but I have no idea whether it's correct or not:

Code:
const catagory = async() => {
  try {
    let movie = document.querySelector(".catagory");
    const mediaQuery = window.matchMedia('(min-width: 992px)')
    var picture = document.getElementsByClassName('.movies');

    function media(mediaQuery) {
      if (mediaQuery.matches) {
        movie.classList.add("movie:nth-child(odd) ")
      }
    }
    movie.classList.add("movie");
    let data = await fetch("http://localhost:3000/movies");
    let res = await data.json();
    let movies = res.map((elem) => {
      return `
        <div class="picture">
          <div class="overlay"></div>
          <img src=${elem.image} alt=${elem.alt}>
        </div>
      `;
    });
    document
      .querySelector(".movies")
      .insertAdjacentHTML("beforeend", movies.join(""));
  } catch (error) {
    console.log(error.message);
  }
};
<p>I want to fetch data but I want it to show less data each time the screen gets smaller. How can I do that?</p>
<p>I tried this, but I have no idea whether it's correct or not:</p>
<pre><code>const catagory = async() => {
try {
let movie = document.querySelector(".catagory");
const mediaQuery = window.matchMedia('(min-width: 992px)')
var picture = document.getElementsByClassName('.movies');

function media(mediaQuery) {
if (mediaQuery.matches) {
movie.classList.add("movie:nth-child(odd) ")
}
}
movie.classList.add("movie");
let data = await fetch("http://localhost:3000/movies");
let res = await data.json();
let movies = res.map((elem) => {
return `
<div class="picture">
<div class="overlay"></div>
<img src=${elem.image} alt=${elem.alt}>
</div>
`;
});
document
.querySelector(".movies")
.insertAdjacentHTML("beforeend", movies.join(""));
} catch (error) {
console.log(error.message);
}
};
</code></pre>
Continue reading...
 

Latest posts

Top