Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Rick and Morty Search</title>
  <link rel="stylesheet" href="styles.css">
</head>

<body>
  <center>Check: <a target="_blank" href="https://inquir.org">Inquir</a></center>
  <form id="search-form">
    <input type="text" id="search-input" placeholder="Search Rick and Morty characters">
    <button type="submit" id="search-button">Search</button>
  </form>
  <div id="results"></div>
  <script src="script.js"></script>
</body>

</html>
              
            
!

CSS

              
                body {
  font-family: Arial, sans-serif;
}

#search-form {
  text-align: center;
  margin-top: 20px;
}

#search-input {
  width: 300px;
  height: 20px;
  padding: 10px;
  font-size: 16px;
}

#search-button {
  padding: 10px 20px;
  font-size: 16px;
  cursor: pointer;
}

#results {
  display: flex;
  flex-wrap: wrap;
}

.result-item {
  width: calc(100% / 3);
  padding: 10px;
  box-sizing: border-box;
  border: 1px solid #ccc;
  border-radius: 8px;
  display: flex;
}

.character-image {
  width: 200px;
  height: auto;
}

.character-info {
  flex-grow: 1;
}

              
            
!

JS

              
                /**
 *
 * YOU CAN READ MORE
 *
 * ON
 *
 * SITE: https://inquir.org
 */

const searchForm = document.getElementById("search-form");
const searchInput = document.getElementById("search-input");
searchForm.addEventListener("submit", function (event) {
  event.preventDefault();
  performSearch(searchInput.value);
});

document.addEventListener("DOMContentLoaded", () => {
  performSearch("");
});

let abortController = new AbortController();
const SIZE = 12;
const getResults = async (q, page = 1) => {
  abortController.abort();
  abortController = new AbortController();
  const body = {
    q: q ?? "",
    indexName: "rnm2",
    size: SIZE,
    page,
    ignoreMetadata: true,
    typoTolerant: false // try changing to true/false
  };

  try {
    const response = await fetch(
      `https://platform.inquir.org/api/v1/search/query`,
      {
        signal: abortController.signal,
        headers: {
          Authorization: `Bearer b02abe1a-xxxxx-12c860ac69e628d032`,
          "Content-Type": "application/json"
        },
        method: "POST",
        body: JSON.stringify(body)
      }
    );

    return response.ok ? await response.json() : {};
  } catch (error) {
    console.error("Fetch error:", error);
    return {};
  }
};

async function performSearch(query, page = 1) {
  const { result } = await getResults(query, page);
  console.log(result);
  if (!result) {
    console.error("Search error:", error.message);
    resultsContainer.innerHTML = `<p>Error: ${error.statusText}</p>`;
    return;
  }
  renderResults(result);
}

function renderResults(data) {
  const resultsContainer = document.getElementById("results");
  resultsContainer.innerHTML = "";
  if (data.results.length > 0) {
    data.results.forEach((item) => {
      const character = item.metadata.character;
      const itemElement = document.createElement("div");
      itemElement.className = "result-item";
      itemElement.innerHTML = `
                <div class="character-card">
                    <img src="${character.image}" alt="${character.name}" class="character-image">
                    <div class="character-info">
                        <h4>${character.name}</h4>
                        <p><strong>Species:</strong> ${character.species}</p>
                        <p><strong>Status:</strong> ${character.status}</p>
                        <p><strong>Gender:</strong> ${character.gender}</p>
                        <p><strong>Location:</strong> ${character.location.name}</p>
                        <a href="${character.url}" target="_blank">View Profile</a>
                    </div>
                </div>
            `;
      resultsContainer.appendChild(itemElement);
    });
  } else {
    resultsContainer.innerHTML =
      "<p>No characters found. Try another search!</p>";
  }
}

              
            
!
999px

Console