Code Monkey home page Code Monkey logo

jquerybem's Introduction

jQuery BEM 0.0.1

jQuery BEM provides a simple API to interact with BEM classes using jQuery.

Usage

There are a number of methods provided by this plugin to make it easier to interact with BEM classes.

addBEMClass

Simply call this method on your jQuery object and pass a BEM class to add to the DOM elements. These classes should include the Block and Element (if present).

Only one BEM class may be applied to any given DOM element, calling this method on an element which already has a Block class attached, or passing a BEM class containing white space, will cause and error. These restrictions help enforce BEM best practices.

// now has class 'body__finger'
var $appendage = $('<div>').addBEMClass('body__finger');

removeBEMClass

Call this method, passing the BEM class attached to the DOM element in order to remove it. It will also remove all classes that combine this with any suffix.

Calling this method with a class name which is not attached to the DOM element will not cause an error, but passing a class name containing white space will.

To remove the BEM class set above:

// now has no class
$appendage.removeBEMClass('body__finger');

hasBEMClass

This method returns a boolean as to whether or not the first DOM element in the jQuery object has the BEM class passed.

Causes an error if the class passed contains white space.

// returns false
$appendage.hasBEMClass('body__toe');

toggleBEMClass

Call this method, passing a BEM class (or space-separated list of BEM classes), to be toggled on the DOM elements.

Passing an class containing white space, or causing more than one BEM class to a DOM element, will cause an error to be thrown. The DOM elements and BEM classes will be processed in order.

// now has class 'body__toe'
$appendage.toggleBEMClass('body__toe');

addBEMSuffix

Call this method, passing a BEM suffix (or space-separated list of BEM suffices), to be added to the DOM elements.

Calling this method on a where the DOM elements have not had BEM classes added via the addBEMClass method will cause an error.

// now has classes 'body__toe', 'body__toe--left-foot', 'body__toe--middle'
$appendage.addBEMSuffix('left-foot middle');

removeBEMSuffix

Call this method, passing a BEM suffix (or space-separated list of BEM suffices), to be removed from the DOM elements.

// now has classes 'body__toe', 'body__toe--middle'
$appendage.removeBEMSuffix('left-foot');

hasBEMSuffix

This method returns a boolean as to whether or not the first DOM element in the jQuery object has the suffix passed.

Causes an error if the suffix passed contains white space.

// returns false
$appendage.hasBEMSuffix('little');

toggleBEMSuffix

Call this method, passing a BEM suffix (or space-separated list of BEM suffices), to be toggled on the DOM elements.

Passing an suffix containing white space will cause an error to be thrown.

// now has classes 'body__toe', 'body__toe--little'
$appendage.toggleBEMSuffix('little middle');

jquerybem's People

Contributors

joshuabambrick avatar

Watchers

 avatar

jquerybem's Issues

allow more flexible ordering in `$.fn.toggleBEMClass`

Would be good if you only ensured that the resulting element has only one BEM class instead of having to maintain this while the method is running.

For example, the following should be valid:

$('<div>').addBEMClass('hand').toggleBEMClass('foot hand');

Currently, this will cause an error as you attempt to add foot before removing hand.

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.