<div class="app">
  <form>
    <input type="text" placeholder="Enter artist or song">
    <button>Search</button>
  </form>

  <div class="result">
    <p>Results will be displayed here</p>
  </div>

  <div class="more"></div>
</div>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  min-height: 100vh;
  background: radial-gradient(circle, lightgreen, darkgreen);
  font-family: helvetica;
  color: #222;
}

.app {
  margin: 1em auto;
  padding: 0.5em;
  width: 320px;
  display: flex;
  flex-direction: column;
  align-items: center;
  background: linear-gradient(darkorange, hotpink);
  border-radius: 5px;
  box-shadow: 0 2px 2px rgba(0, 0, 0, 0.6);
}

input {
  margin: 0.5em;
  padding: 0.4em 0.6em;
  border: none;
  border-radius: 3px;
  box-shadow: inset 0 0 2px rgba(0, 0, 0, 0.6);
  font-size: 0.9em;
  text-align: center;
}

input:focus {
  outline: 2px solid #222;
}

ul {
  list-style: none;
}

li {
  margin: 0.5em 0;
  text-align: center;
}

button {
  margin: 0.5em auto;
  padding: 0.4em 0.6em;
  width: 85px;
  border: none;
  outline: none;
  border-radius: 3px;
  background: linear-gradient(skyblue, steelblue);
  box-shadow: 0 2px 2px rgba(0, 0, 0, 0.6);
  font-size: 0.9em;
  text-align: center;
  cursor: pointer;
}

button:active {
  color: #ddd;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.8);
}

h3 {
  margin: 0.5em 0;
  text-align: center;
}

p {
  line-height: 1.6em;
}
const form = document.querySelector("form");
const search = document.querySelector('input[type="text"]');
const result = document.querySelector(".result");
const more = document.querySelector(".more");

const url = "https://api.lyrics.ovh";

async function searchSongs(text) {
  const response = await fetch(`${url}/suggest/${text}`);
  const data = await response.json();

  showData(data);
}

function showData(data) {
  result.innerHTML = `
        <ul>
            ${data.data
              .map(
                (song) =>
                  `<li><p>${song.artist.name} - ${song.title}</p><button data-artist="${song.artist.name}" data-song="${song.title}">Get Lyrics</button></li>`
              )
              .join("")}
        </ul>
    `;

  if (data.prev || data.next) {
    more.innerHTML = `
            ${
              data.prev
                ? `<button onclick="getMore('${data.prev}')">Prev</button>`
                : ""
            }
            ${
              data.next
                ? `<button onclick="getMore('${data.next}')">Next</button>`
                : ""
            }
        `;
  } else more.innerHTML = "";
}

async function getMore(url) {
  const response = await fetch(`https://cors-anywhere.herokuapp.com/${url}`);
  const data = await response.json();

  showData(data);
}

async function getLyrics(artist, song) {
  const response = await fetch(`${url}/v1/${artist}/${song}`);
  const data = await response.json();

  if (data.error) {
    result.innerHTML = data.error;
  } else {
    const lyrics = data.lyrics
      .replace(/\r{2,}|\n{2,}/g, "\n")
      .replace(/\r\n|\r|\n/g, "<br>");

    result.innerHTML = `<h3>${artist} - ${song}</h3><p>${lyrics}<p>`;
  }

  more.innerHTML = "";
}

form.addEventListener("submit", (e) => {
  e.preventDefault();

  const text = search.value.trim();

  if (!text) {
    return;
  } else searchSongs(text);
});

result.addEventListener("click", (e) => {
  if (e.target.tagName === "BUTTON") {
    const artist = e.target.dataset.artist;
    const song = e.target.dataset.song;

    getLyrics(artist, song);
  }
});
Run Pen

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.