Write dynamic HTML page using JavaScript with panes (tiles) contains paragraphs which defined in javascript list. On the top of page there is search field which searches tiles by substrings. Search is case insensetive. Then display only relevant tiles for search
Assistant
User
Thank you. It's awesome. Can you make highlighting for found substrings in tiles?
Assistant
User
Can you make search box little smaller?
Assistant
User
Thank you! Okay. Now I'll give slightly different task: add smooth sliding and shrinking animations when tiles appear and disappear
Assistant
Creating a dynamic HTML page with search functionality that filters tiles (panes) based on a JavaScript list can be achieved easily. Below is a simple implementation that demonstrates this concept using basic HTML, CSS, and JavaScript.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dynamic Tiles with Search</title>
<!-- Basic CSS for styling -->
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
}
input[type="text"] {
width: 100%;
padding: 10px;
margin-bottom: 20px;
font-size: 16px;
}
.tiles {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 20px;
}
.tile {
background-color: #f9f9f9;
border: 1px solid #ddd;
border-radius: 5px;
padding: 15px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
transition: transform 0.2s;
}
.tile:hover {
transform: scale(1.05);
}
</style>
</head>
<body>
<h1>Search Tiles</h1>
<input type="text" id="searchInput" placeholder="Search for tiles..." onkeyup="filterTiles()">
<div class="tiles" id="tilesContainer">
<!-- Dynamic tiles will be populated here -->
</div>
<script>
// Sample data: an array of paragraphs
const paragraphs = [
"This is the first tile.",
"Here is the second tile.",
"Tile number three is different.",
"This is the fourth idea.",
"Fifth tile, not very interesting.",
"Here's another tile for good measure.",
"The seventh tile contains different content.",
"Eighth tile showcases something unique.",
"Ninth tile is about JavaScript.",
"Tenth tile focuses on dynamic content."
];
// Function to render tiles
function renderTiles(filteredParagraphs) {
const tilesContainer = document.getElementById('tilesContainer');
tilesContainer.innerHTML = '';
filteredParagraphs.forEach(paragraph => {
const tile = document.createElement('div');
tile.className = 'tile';
tile.textContent = paragraph;
tilesContainer.appendChild(tile);
});
}
// Function to filter tiles based on search input
function filterTiles() {
const searchInput = document.getElementById('searchInput').value.toLowerCase();
const filteredParagraphs = paragraphs.filter(paragraph =>
paragraph.toLowerCase().includes(searchInput)
);
renderTiles(filteredParagraphs);
}
// Initial render of all tiles
renderTiles(paragraphs);
</script>
</body>
</html>
Explanation:
HTML Structure:
A search input field for searching tiles.
A div element (tilesContainer) where the tiles will be dynamically displayed.
CSS Styles:
Basic styles are applied to make the tiles look presentable and to lay them out in a responsive grid.
JavaScript Logic:
An array (paragraphs) contains the text for each tile.
renderTiles(filteredParagraphs): This function dynamically creates div elements for each tile in the provided list of paragraphs and adds them to the tilesContainer.
filterTiles(): This function reads the search input value, filters the paragraphs array according to the input, and calls renderTiles to update the displayed tiles. The filtering is case insensitive.
Interactivity: The tiles get updated in real-time as the user types in the search input field, making for a smooth user experience.
You can simply copy this code into an HTML file and open it in a web browser to see it in action.
Certainly! To highlight the found substrings in the tiles, you can modify the rendering function to wrap the matched portions of the paragraph in a <span> with a specific class that applies a highlight style. Below is the modified version of the initial code that includes this highlighting feature.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dynamic Tiles with Search and Highlighting</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
}
input[type="text"] {
width: 100%;
padding: 10px;
margin-bottom: 20px;
font-size: 16px;
}
.tiles {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 20px;
}
.tile {
background-color: #f9f9f9;
border: 1px solid #ddd;
border-radius: 5px;
padding: 15px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
transition: transform 0.2s;
}
.tile:hover {
transform: scale(1.05);
}
.highlight {
background-color: yellow; /* Highlight color */
font-weight: bold;
}
</style>
</head>
<body>
<h1>Search Tiles</h1>
<input type="text" id="searchInput" placeholder="Search for tiles..." onkeyup="filterTiles()">
<div class="tiles" id="tilesContainer">
<!-- Dynamic tiles will be populated here -->
</div>
<script>
// Sample data: an array of paragraphs
const paragraphs = [
"This is the first tile.",
"Here is the second tile.",
"Tile number three is different.",
"This is the fourth idea.",
"Fifth tile, not very interesting.",
"Here's another tile for good measure.",
"The seventh tile contains different content.",
"Eighth tile showcases something unique.",
"Ninth tile is about JavaScript.",
"Tenth tile focuses on dynamic content."
];
// Function to render tiles with highlighted search terms
function renderTiles(filteredParagraphs, searchTerm) {
const tilesContainer = document.getElementById('tilesContainer');
tilesContainer.innerHTML = '';
filteredParagraphs.forEach(paragraph => {
const tile = document.createElement('div');
tile.className = 'tile';
tile.innerHTML = highlightText(paragraph, searchTerm);
tilesContainer.appendChild(tile);
});
}
// Function to highlight the search term in the paragraph
function highlightText(text, searchTerm) {
if (!searchTerm) return text; // Return unmodified text if no search term
const regex = new RegExp(`(${searchTerm})`, 'gi'); // Create regex for case-insensitive matching
return text.replace(regex, '<span class="highlight">$1</span>'); // Highlight match
}
// Function to filter tiles based on search input
function filterTiles() {
const searchInput = document.getElementById('searchInput').value.toLowerCase();
const filteredParagraphs = paragraphs.filter(paragraph =>
paragraph.toLowerCase().includes(searchInput)
);
renderTiles(filteredParagraphs, searchInput); // Pass the search term for highlighting
}
// Initial render of all tiles
renderTiles(paragraphs, '');
</script>
</body>
</html>
Key Changes Made:
Highlighting Functionality:
Added the highlightText(text, searchTerm) function that takes a paragraph and the search term as inputs. It uses a regular expression to wrap matches in a <span> with a class name highlight.
Highlight CSS Class:
The .highlight class was added to the CSS with a yellow background color and bold text to make the matched substrings visually stand out.
Adjustments to renderTiles:
The renderTiles function now receives a second argument, searchTerm, which is passed to the highlightText function to apply highlighting as it renders each tile.
Experience:
With these changes, when a user searches for text, any matching substring in the tile paragraphs will be highlighted, enhancing the user experience by making it easier to see why each tile is relevant to the search query. You can simply copy this modified code into your HTML file and see it in action!
Certainly! To make the search box smaller, you can reduce its width and padding in the CSS styles. Below is the updated HTML code with adjustments to the search box, making it more compact:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dynamic Tiles with Search and Highlighting</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
}
input[type="text"] {
width: 300px; /* Reduced width */
padding: 8px; /* Reduced padding */
margin-bottom: 20px;
font-size: 16px;
}
.tiles {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 20px;
}
.tile {
background-color: #f9f9f9;
border: 1px solid #ddd;
border-radius: 5px;
padding: 15px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
transition: transform 0.2s;
}
.tile:hover {
transform: scale(1.05);
}
.highlight {
background-color: yellow; /* Highlight color */
font-weight: bold;
}
</style>
</head>
<body>
<h1>Search Tiles</h1>
<input type="text" id="searchInput" placeholder="Search for tiles..." onkeyup="filterTiles()">
<div class="tiles" id="tilesContainer">
<!-- Dynamic tiles will be populated here -->
</div>
<script>
// Sample data: an array of paragraphs
const paragraphs = [
"This is the first tile.",
"Here is the second tile.",
"Tile number three is different.",
"This is the fourth idea.",
"Fifth tile, not very interesting.",
"Here's another tile for good measure.",
"The seventh tile contains different content.",
"Eighth tile showcases something unique.",
"Ninth tile is about JavaScript.",
"Tenth tile focuses on dynamic content."
];
// Function to render tiles with highlighted search terms
function renderTiles(filteredParagraphs, searchTerm) {
const tilesContainer = document.getElementById('tilesContainer');
tilesContainer.innerHTML = '';
filteredParagraphs.forEach(paragraph => {
const tile = document.createElement('div');
tile.className = 'tile';
tile.innerHTML = highlightText(paragraph, searchTerm);
tilesContainer.appendChild(tile);
});
}
// Function to highlight the search term in the paragraph
function highlightText(text, searchTerm) {
if (!searchTerm) return text; // Return unmodified text if no search term
const regex = new RegExp(`(${searchTerm})`, 'gi'); // Create regex for case-insensitive matching
return text.replace(regex, '<span class="highlight">$1</span>'); // Highlight match
}
// Function to filter tiles based on search input
function filterTiles() {
const searchInput = document.getElementById('searchInput').value.toLowerCase();
const filteredParagraphs = paragraphs.filter(paragraph =>
paragraph.toLowerCase().includes(searchInput)
);
renderTiles(filteredParagraphs, searchInput); // Pass the search term for highlighting
}
// Initial render of all tiles
renderTiles(paragraphs, '');
</script>
</body>
</html>
Adjustments Made:
Search Box Width: Changed the width property of the search input from 100% to 300px.
Search Box Padding: Reduced the padding from 10px to 8px.
You can adjust the width and padding values even further to suit your preferences. Copy this updated code into your HTML file, and you'll see a smaller search box on the page!
To add smooth sliding and shrinking animations to the tiles as they appear and disappear, you can use CSS transitions and animations. We'll modify the CSS to include classes that handle the animations and adjust the JavaScript to apply these classes appropriately when tiles are rendered.
Here's the updated code:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dynamic Tiles with Search and Highlighting</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
}
input[type="text"] {
width: 300px; /* Reduced width */
padding: 8px; /* Reduced padding */
margin-bottom: 20px;
font-size: 16px;
}
.tiles {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 20px;
transition: opacity 0.3s ease, transform 0.3s ease;
opacity: 1;
}
.tile {
background-color: #f9f9f9;
border: 1px solid #ddd;
border-radius: 5px;
padding: 15px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
transform: translateY(20px); /* Start position for animation */
opacity: 0; /* Start opacity */
transition: transform 0.3s ease, opacity 0.3s ease; /* Animation properties */
}
.tile.visible {
transform: translateY(0); /* Reset position */
opacity: 1; /* Show tile */
}
.highlight {
background-color: yellow; /* Highlight color */
font-weight: bold;
}
</style>
</head>
<body>
<h1>Search Tiles</h1>
<input type="text" id="searchInput" placeholder="Search for tiles..." onkeyup="filterTiles()">
<div class="tiles" id="tilesContainer">
<!-- Dynamic tiles will be populated here -->
</div>
<script>
// Sample data: an array of paragraphs
const paragraphs = [
"This is the first tile.",
"Here is the second tile.",
"Tile number three is different.",
"This is the fourth idea.",
"Fifth tile, not very interesting.",
"Here's another tile for good measure.",
"The seventh tile contains different content.",
"Eighth tile showcases something unique.",
"Ninth tile is about JavaScript.",
"Tenth tile focuses on dynamic content."
];
// Function to render tiles with highlighted search terms
function renderTiles(filteredParagraphs, searchTerm) {
const tilesContainer = document.getElementById('tilesContainer');
tilesContainer.innerHTML = ''; // Clear existing tiles
filteredParagraphs.forEach(paragraph => {
const tile = document.createElement('div');
tile.className = 'tile';
tile.innerHTML = highlightText(paragraph, searchTerm);
tilesContainer.appendChild(tile);
// Use requestAnimationFrame for smooth animations
requestAnimationFrame(() => {
tile.classList.add('visible'); // Add visible class to trigger animations
});
});
}
// Function to highlight the search term in the paragraph
function highlightText(text, searchTerm) {
if (!searchTerm) return text; // Return unmodified text if no search term
const regex = new RegExp(`(${searchTerm})`, 'gi'); // Create regex for case-insensitive matching
return text.replace(regex, '<span class="highlight">$1</span>'); // Highlight match
}
// Function to filter tiles based on search input
function filterTiles() {
const searchInput = document.getElementById('searchInput').value.toLowerCase();
const filteredParagraphs = paragraphs.filter(paragraph =>
paragraph.toLowerCase().includes(searchInput)
);
renderTiles(filteredParagraphs, searchInput); // Pass the search term for highlighting
}
// Initial render of all tiles
renderTiles(paragraphs, '');
</script>
</body>
</html>
Key Changes Made:
Tile Animation:
CSS Transitions: The tile class includes transform and opacity properties that will be animated when the tile is rendered.
Sliding Effect: The tiles start from a position of 20px downwards and are initially transparent (opacity: 0). The class visible resets this position to 0 and changes the opacity to 1 for a sliding and fading effect.
JavaScript Update:
In the renderTiles function, requestAnimationFrame is used to ensure that the visible class is added to the tile elements after they are appended, triggering a smooth animation.
Experience:
With these additions, the tiles will slide up and fade in smoothly when they appear. When you type in the search box, relevant tiles will animate into view, providing a more engaging user experience. You can copy this code into your HTML file to see the effect in action!