views:

39

answers:

2

Thanks all for the help, rod.

Hi All,

I have a div that contains many spans and each of those spans contains a single href.

Basically it's a tag cloud. What I'd like to do is have a textbox that filters the tag cloud on KeyUp event.

Any ideas or is this possible?

Updated question: What would be the best way to reset the list to start the search over again?

Thanks, rodchar

+3  A: 

Basically, what you want to do is something like this

$('#myTextbox').keyup(function() {
    $('#divCloud > span').not('span:contains(' + $(this).val() + ')').hide();
});
David Hedlund
+1. Change `$(this).val()` to `this.value`, please.
David Murdoch
@David Murdoch: that can only be safely done to the extent that all browsers work the same way in this regard (the fact that all major browsers do is incidental, and not guaranteed to remain that way). the route via jquery ensures cross-browser compatibility, which is really the biggest advantage with using a javascript framework at all.
David Hedlund
um, what? `this.value` on `<input type='text' />` will _always_ return the `value` property/attribute of the input or an empty string (`""`).
David Murdoch
@David Murdoch: currently, yes, because that's how javascript is implemented in all major browsers. but the main feature of jQuery is to handle all browser differences. if a new browser appears out of the blue that requires you to write `this.val` instead of `this.value`, then jQuery `val()` will be updated to accommodate that, and all you have to do to ensure continued cross-browser support is to make sure you've got the lates version of jQuery, as opposed to change all your use of `this.value` throughout the code.
David Hedlund
That won't ever happen...ever. ever. ever. There is no need (in this case) to ask jQuery to call the 5+ functions involved in `$().val()` just to finally perform: `return (elem.value || "").replace(rreturn, "");` (line 1390 of jquery-1.4.2.js)
David Murdoch
ie6 should never have happened. that doesn't always prevent things from happening.
David Hedlund
Thanks for the feedback everyone, rod.
rod
Anyway to speed this up on IE it drags when there's about 1300 tags in cloud (does fine in Firefox), rod.
rod
A: 

This can probably be improved upon and made lighter but this at least gives the functionality of being able to hide multiple tags by seperating your input by commas. For example: entering this, that, something into the input will hide each of those spans.

Demo HTML:

<div id="tag_cloud">
    <span>this</span>
    <span>that</span>
    <span>another</span>
    <span>something</span>
    <span>random</span>
</div>

<input type="text" id="filter" />

Demo jQuery:

function oc(a){
    var o = {};
    for(var i=0;i<a.length;i++){
        o[a[i]]='';
    }

    return o;
}

$(function(){
    $('#filter').keyup(function(){
        var a = this.value.replace(/ /g,'').split(',');

        $('#tag_cloud span').each(function(){
            if($(this).text() in oc(a)){
                $(this).hide();
            }
            else {
                $(this).show();
            }
        })
    })
})
Sam