Kyle Kyle - 1 year ago 105
Ajax Question

Use AJAX or similar technology to narrow down already existing list

Hopefully I explain this to where it makes sense, the most I could find by searching terms like I used in the title gave plenty of autocomplete examples, but nothing quite what I'm looking for. I have a list of buttons (they're coded as inputs right now) and I want to add a search field that will narrow down the buttons as the user types in a search field.

Say for example, I have 30 buttons with popular websites. If a person wanted to pull Google, they'd start typing it out which would start by including everything with the letter "G" in it, then "O", etc. Everything else would "disappear" from the page.

I can sort of think of a way to do this manually, but I think my code wouldn't be DRY. Possibly set an "on" and "off" ID, and use CSS to display:none or something to that effect.

I think the best way to do this would be via AJAX, but there may be some javascript voodoo more applicable.

Answer Source

To easy. At first, its unneccessary to filter the answers serverside, if all the data is already at the users. Also, you shouldnt write html and filter it with js, you should write it in js and generate an html output. Lets start with the structure:

var links=[

Now generate the links in html:

    var container=document.body;//change this to your needs
     var link=links[i];

If sth is inputed, hide the the unmatched ones:

function filter(string){
 //loop trough links
    var link=links[i];
    //if string doesnt match name

Use like this:


You could bind that to an input:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download