Utilize AJAX or another advanced technology to refine a pre-existing list

I am trying to implement a searchable list of buttons on my website, but I haven't been able to find a solution that fits exactly what I have in mind. Currently, I have a list of buttons coded as inputs and I want to add a search field that will filter the buttons as the user types.

For example, if I have 30 buttons representing popular websites and a user starts typing "G", only buttons with names starting with "G" should be displayed, and so on for subsequent letters entered by the user.

I have considered manually coding a solution using IDs and CSS to show/hide elements, but I feel this approach may not be very efficient or maintainable in the long run.

While AJAX seems like a good option for dynamic filtering, I am open to exploring other JavaScript solutions that might work better in this scenario.

Answer №1

Creating dynamic links in JavaScript:

var links=[
  {
       name:"google",
       url:"http://google.com"
   },
   {nextone}
   ];

Generate the links in HTML using JavaScript:

window.onload=function(){
    var container=document.body;//change this to your needs
  for(i=0;i<links.length;i++){
     var link=links[i];
     link.html=document.createElement("a");
     link.html.innerHTML=link.name;
     link.html.src=link.url;
     container.appendChild(link.html);
 }
 };

If a search term is inputted, hide the unmatched links:

function filter(string){
 //loop through links
 for(i=0;i<links.length;i++){
    var link=links[i];
    //if string doesn't match name
    if(!link.name.split(string)[1]){
       link.html.style.display="none";
     }else{
       link.html.style.display="block";
     }
  }
  }

Applying the filter function:

filter("goo");

You could bind the filter to an input element:

yourinput.addEventListener("onchange",function(){filter(this.value)},false);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

ReactJS sidebar navigation functionality

I have a fixed sidebar with icons for navigating to different pages. When an icon is clicked, a secondary menu slides out. However, the issue is that when another icon is selected, the menu slides back in instead of switching or staying out. Additionally, ...

"Utilize Ajax to load PHP content and dynamically refresh a specific div

I have implemented an image uploading system, but now I want to incorporate a feature that allows users to rotate the uploaded images using Ajax. The challenge I'm facing is that if the session variable is lost during a full page update, I need to ens ...

Is there a way to implement a function in Javascript or CSS where hovering over a button will cause a div to scroll either left or right

I am working on creating a unique photo gallery layout with a description block positioned below the images. My goal is to incorporate two arrow buttons, one on each side of the photos, that will trigger a scrolling effect when hovered over - shifting the ...

Which is the preferable option for creating a circular daily planner: canvas or SVG?

As a novice programmer delving into the world of coding (or at least hoping to), I have a question regarding graphic tools in html5 for my latest project. My goal is to create a planner app using electron, with the unique twist that the planner form sho ...

Ways to update a single column in an HTML table when there is a change

I'm stuck trying to find a more efficient method for updating a column in an html table without having to reload the entire table. The table consists of player stats, all pulled from my MYSQL database and displayed in the table except for the last col ...

Reload Popup Box

I am currently developing a website using Django and I am in need of a popup window that can display logging messages and automatically refresh itself every N seconds. In order to achieve this, I am utilizing the standard Python logger, JavaScript, and Daj ...

Is it important to position elements based solely on the parent container?

My frustration with element positioning persists: <div id="container"> <div id="div1"></div> <div id="div2"></div> <div id="div3"></div> </div> #div1 { right:0; // I want its right border to be 0px from th ...

Dynamic inheritance in Node.js based on the version being used

Why does the code provided only function correctly in Node.js versions 5.x and 6.x, but not in versions 4.x and older? Is there a way to modify the code so that it can work across Node.js versions 0.10.x - 6.x? 'use strict'; var util = require ...

What is the best way to convert items from a foreach loop into a JSON string using the json_encode() function in PHP?

I want to populate a string with all the emails fetched from the database, in order to use JavaScript for checking if the email entered by a user in a form field is already registered. I'm attempting to utilize the json_encode() function. $connec ...

Using axios to render data in a view template

I am currently working on developing a basic Single Page Application (SPA) with the help of vue.js and axioz for scripts, without using CLI or any other tools. At this point, I have succeeded in fetching data from a JSON file, rendering and paginating the ...

I'm having trouble pinpointing the issue in my code

For some reason, the first button in the div in this code is not working on HTML files. I have tried multiple JavaScript and HTML validators but none of them seem to work. Surprisingly, it works fine on codecademy.com and w3schools.com, but the issue persi ...

Unable to achieve separation of borders within dash_table.DataTable styling using CSS by setting border-collapse: separate; considering using border-spacing instead

Is there a way to create space between the lines of a table in dash_table.DataTable using css or regular style capabilities? I have tried using border-collapse and border-spacing but it doesn't seem to be working. How can I achieve this desired spacin ...

Ways to assign a default background shade to a webpage with a transparent background

My page has a completely transparent background achieved by using: body { background:none transparent!important; } When I display this page in a transparent iframe on another site, I can see the website's background through the page. However, if ...

I am attempting to rebuild Vuex's Getting Started example by utilizing multiple components, yet I am struggling to understand how to call root methods from within these components

The project I'm working on can be found here. It is a simple one, but for the purpose of learning, I divided it into index and four js files (parent, child, root, and store). My challenge lies in figuring out how to call the increment and decrement ro ...

Navigating Error: net::ERR_CONNECTION while utilizing Puppeteer

I attempted to utilize a proxy from the following site: Below is my Puppeteer scraping code (deployed on Heroku), encountering an error at the .goto() method, as mentioned in the title: const preparePageForTests = async (page) => { const userAgent = & ...

Error Notification in React Bootstrap - Stay Informed!

When a 401 error is returned from the API, I need to show an error message in the component. My approach involves using an unbound state and ES6. However, I encountered this error: Cannot read property 'setState' of undefined Below is the login ...

Ugly consequences arise as Gulp stumbles upon uncharted territory during the uglify

I'm experiencing an issue with my squish-jquery task. Every time I run it, I encounter the following error: Starting 'squish-jquery'... events.js:85 throw er; // Unhandled 'error' event ^ Error at new JS_Par ...

Find similarities between 2 observable arrays and store them in a new array using knockout data binding

There are three observable arrays set up as follows: a [1,3] b [ {"ID": 1, "Val": "Value1"}, {"ID":2, "Val":"Value2"}, {"ID":3, "Val":"Value3"}] c [] The goal is to compare array a with the IDs of elements in array b and then push the entire value of arr ...

Text centered on hover for figure caption

Check out this fiddle http://jsfiddle.net/sLdhsbou/ where I am trying to center the "x" that appears on hover perfectly no matter what size the image is. Also, why does the transition not occur when moving the mouse outside of the figure, unlike when movi ...

Why is AJAX returning false and I'm unable to figure out the reason?

My goal is to perform a database query for a keyword instantly upon input change. Currently, I am able to successfully execute the query and store all the results. However, when attempting to display the results using GET, my ajax function returns false. W ...