GithubHelp home page GithubHelp logo

foo123 / selectorlistener Goto Github PK

View Code? Open in Web Editor NEW

This project forked from csuwildcat/selectorlistener

1.0 3.0 0.0 86 KB

Listen for CSS selector rule matches (including selectors for newly added and/or already existing elements, removed elements) at document or element level

Home Page: http://nikos-web-dev.000webhostapp.com

License: Other

HTML 19.92% JavaScript 80.08%

selectorlistener's Introduction

SelectorListener

Provides the following document/element methods to enable listening for CSS selector rule matches:

note this reference inside callbacks reference the matched element (i.e event.target) and not the element the listener is attached to (similar to jQuery use)

The Basics

var oneTwoThree = function(){
  alert('Listening for complex element sequences is easy as 1, 2, 3!');
};

document.addSelectorListener('.one + .two + .three', oneTwoThree);

document.removeSelectorListener('.one + .two + .three', oneTwoThree);

/* Also available on elements: */

document.getElementById('foo').addSelectorListener('.one + .two + .three', oneTwoThree);

document.getElementById('foo').removeSelectorListener('.one + .two + .three', oneTwoThree);

Now let's get fancy:

// Listening for attribute value matches? Child's play.
document.addSelectorListener('.foo[bar="boom"]', function(){ ... });

// Matching elements on hashchange can be annoying, let's make it stupid simple
document.addSelectorListener('*:target', function(event){
  alert('The hash-targeted element is:' + event.target);
});

// How about a more performant way to listen for custom tooltip nodes document wide?
document.addSelectorListener('.tooltip:hover', function(){ ... });


/*** Now that we have the new CSS 4 Selector spec, let's see what we can do: ***/

// Working with HTML5 sliders just got even easier
document.querySelector('#RandomForm').addSelectorListener('slider:out-of-range', function(){
  alert('Your slider value is now out of range! Oh noes!');
});

Select only newly added and/or already existing elements (DOM mutation-like events):

document.addSelectorListener('.foo[bar="boom"]::added', function(){ ... });

// this also works
document.addSelectorListener('.foo[bar="boom"]:not(::exists)', function(){ ... });


document.addSelectorListener('*:exists:target', function(){ ... });

Select elements which have their css-class changed (either a css class added or removed) (DOM mutation-like events):

document.addSelectorListener('.foo[bar="boom"]::class-added(.a-class)', function(){ ... });

document.addSelectorListener('.foo[bar="boom"]::class-removed(.a-class)', function(){ ... });

Select only existing elements that have been removed (DOM mutation-like events, experimental):

document.addSelectorListener('.foo[bar="boom"]::removed', function(){ ... });

(see index.html)

selectors matched screenshot

Browser Support (tested)

  1. Firefox 40+
  2. Chrome 41+
  3. Opera 34+
  4. IE 11+

selectorlistener's People

Contributors

csuwildcat avatar alfred-landrum avatar foo123 avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.