GithubHelp home page GithubHelp logo

attributelist's Introduction

AttributeList.js

Manipulate HTML attributes with a classList like API.

Why

Write about how cumberstone is to manually Element.getAttribute('attrname'), modify it as string, keep a copy to check if the value that your adding already exists, taking care of spaces and other characters and finally Element.setAttribute('attrnam', 'modifiedCopy') takes more effort than it should.

Also I'm a fan of <body data-js-scope="menu-is-open"> kind of stuff and prefer to leave classes to styling.

Setup

  1. Clone
  2. npm install
  3. npm start

Usage

As a wrapper (loadash, jqueryLike)

<div class="my-element" data-caliban-lives="oops hipster-- monster" />
import {$attributeList} from 'attributelist';
// make it globally available under your favourite alias.
window.$al = $attributeList;

/*
 * @function $al(el)
 * @param {HTMLElement|String} el 
 * @returns an object with each attribute than can be accessed as .dataset
*/
$al('.myElement')
// returns {calibanLives: attributeListInstance}

/*
 * Add
 */
$al('.myElement').calibanLives.add('shakespeare')
// <div class="my-element" data-caliban-lives="oops hipster-- monster shakepsear" />

/*
 * Remove
 */
$al('.myElement').calibanLives.remove('oops')
// <div class="my-element" data-caliban-lives="hipster-- monster shakespear" />

/*
 * Toggle
 */
$al('.myElement').calibanLives.toggle('monster')
// <div class="my-element" data-caliban-lives="hipster-- shakespear" />

Dangerous, use at your own risk

You can also extend native Element prototype, if you're felling #savage

<div class="my-element" data-caliban-lives="attributelist is not" />
import {$dangerouslyExtendElementPrototypeWithAttributeList} from 'attributelist';

// you know is dangerous with name like this
dangerouslyExtendElementPrototypeWithAttributeList();

var x = document.querySelector('.my-element');

x.attributeList.calibanLives.add("awesome")
x.attributeList.calibanLives.remove("not")
x.attributeList.calibanLives.toggle("๐Ÿ˜Ž")
// <div class="my-element" data-caliban-lives="attribute is awesome ๐Ÿ˜Ž" />

Play With

A demo is running on port 8080, open your console and an $al() wrapper function is available globally to test

Thanks to open-source

Parcel.js โ€” Unless you've been living under a rock, you probably have heard about the coolest thing that happen to javaScript bundling.

Lea Verou For defying conventions and give me insparation to give a first try on the dangerous path of extending native prototypes. I still don't understand this programming nerdery as i'm not the smartest type of human beeing, but maybe someone in the wild will help me one day.

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.