Skip to content

Instantly share code, notes, and snippets.

@blaedj
Last active November 3, 2016 15:06
Show Gist options
  • Save blaedj/5b9ca7e17edb6bf44175f3f9a7be9233 to your computer and use it in GitHub Desktop.
Save blaedj/5b9ca7e17edb6bf44175f3f9a7be9233 to your computer and use it in GitHub Desktop.
JQuery Selectors

the jquery code $("#searchable.dynamic-content") will match on an element that has the id searchable and the class dynamic-content. That doesn't help us much, since in the code in the issue we are trying to get at an element with the class dynamic-content that is inside an element with the id searchable. Assuming we have the following html:

<div id="searchable">
<div class="dynamic-content">...content here...</div>
</div>

the first selector won't match on anything. It would only match if we added the class dynamic-content to the outer div that has an id of searchable. However, if we add the space between #searchable and .dynamic-content, we are telling jQuery that we want an element with the class dynamic-content that is inside (aka a child of) the element with the id searchable.

In your example, $("#friendlist#friends"), you are telling jquery to look for an element with the id friendlist and the idfriends, which is not a valid selector, since an element should only have one id attribute. If you were to put a space between the two, $("#friendlist #friends"), it would match on the following:

<div id="friendlist">
  <div id="friends"> <!-- Matches this element--></div>
</div>
@blaedj
Copy link
Author

blaedj commented Nov 3, 2016

see this codepen for a working example.

@jehadja
Copy link

jehadja commented Nov 3, 2016

thank you for answer and i have a small suggestion if we can realize it
its to add Destroy method to code and when data come we will rebuild it :)

i write untested vision

`  destroy: function() {
           
           this.unbindEvents();
           this._destroy();
           this.$element.removeData();
       },

 unbindEvents: function() {
       
            var that = this;

            this.$search.off( 'change keyup', function() {
                that.search( $( this ).val() );

                that.updateStriping();
            });

            if ( searchFocusCallback ) {
                this.$search.off( 'focus', this.settings.onSearchFocus );
            }

            if ( searchBlurCallback ) {
                this.$search.off( 'blur', this.settings.onSearchBlur );
            }

            if ( this.$search.val() !== '' ) {
                this.$search.trigger( 'change' );
            }
        },
`

i don't know if it will work but i think it will be much better as you don't grantee all data come through click or keyup etc event
so some cases we cant you Jquery on function

best regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment