FreePromptsAI • Community

Code Snippets

Discover reusable snippets for your next project. Browse, copy, and share free code.

Share a snippet

Accessible search form using HTML

An accessible, keyboard-friendly search form with a visible label.

<form role="search" method="get" action="/search">
  <label for="site-search">Search the library</label>
  <input id="site-search" name="q" type="search" required>
  <button type="submit">Search</button>
</form>

Read and validate JSON in Python

Load a UTF-8 JSON document with clear errors for invalid or missing files.

import json
from pathlib import Path

def load_json(path: str):
    try:
        return json.loads(Path(path).read_text(encoding='utf-8'))
    except (OSError, json.JSONDecodeError) as error:
        raise ValueError(f'Cannot read {path}: {error}') from error

Debounce a search input in JavaScript

Delay a function call until the user stops typing to avoid unnecessary requests.

function debounce(callback, delay = 300) {
  let timer;
  return (...args) => {
    clearTimeout(timer);
    timer = setTimeout(() => callback(...args), delay);
  };
}
const onSearch = debounce((query) => console.log(query));