Skip to content

wsjscss/utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Utils

Util methods to work with DOM.

Warning All methods has console.warn if passed selector is null or undefined. Doesn't work in minified file utils.min.js

Usage

HTML

<button> Click me </button>
<button> I'm awesome </button>

JS

let btns = S.find('button')
S.addClass(btns, 'active')

// Or
S.addClass('button', 'active')

HTML Result

<button class="active"> Click me </button>
<button class="active"> I'm awesome </button>

Methods

Find

Returns array of selected elements

S.each(selector, target)
let btns = S.find('button')
console.log(btns) // [button, button ... ]

// Find button inside parent
S.find('#parent', 'button')

First

Returns First founded clean DOM element without array.

S.first(selector, [childSelector, callback])
let firstButton = S.first('button') // Search in <body>
console.log(btns) // <button>...</button>

// or
let firstButton = S.first('header', button) // Search in <header>
console.log(btns) // <button>...</button>

Each (Chainable)

Loops through founded elements and run callback. Inside callback this will be the the link to each element

S.each(selector, callback)
let btns = S.find('button')
S.each(btns, doSomething)

// Or
S.each('button', doSomething)

function doSomething(){
    console.log( this ) // <button>...</button>
}

Add / Remove / Toggle class (Chainable)

Add class or array of classes to all founded elements

S.addClass(selector, string|array)
// Single class
let btns = S.find('button')
S.addClass(btns, 'active')
S.removeClass(btns, 'active')
S.toggleClass(btns, 'active')

// Array of classes
S.addClass(btns, ['active', 'small'])
S.removeClass(btns, ['active', 'small'])
S.toggleClass(btns, ['active', 'small'])

Has class

Returns true or false. Works only with FIRST founded element.

S.hasClass(selector)
let btnIsActive = S.hasClass('button', 'active')
console.log(btnIsActive) // true or false

On (Chainable)

Adds event listener on selector.

S.on(selector, method, [target,] callback, [options])
S.on('button', 'click', function(){
    S.toggleClass(this, 'active')  // this - reference to native DOM element
}, false)

// Delegate event
S.on('body', 'click', 'button', function(){
    S.toggleClass(this, 'active')  // this - reference to native DOM element
}, false)

Attention: If device is iPhone or iPad, click event will automaticaly change to touchstart on body, html, document

// Click event will change to touchevent automaticaly on iOS
S.on('body', 'click', function(){
    S.toggleClass(this, 'active')
})

Trigger (Chainable)

Dispatches event (native or custom)

S.trigger(selector, event, [object])
// Listen for event 'btnClicked'
document.addEventListener('btnClicked', function () {
    console.log( 'clicked' );
})

S.on('.btn', 'click', function () {
    // Trigger event 'btnClicked'
    S.trigger(document, 'btnClicked', {
        detail: {
            active: true
        }
    })
})

Delegate click to body

Delegate clicks to body. Using any amount of delegates will create only one event on body

S.bodyOnClick(selector, callback)
// Selector - must be string
S.bodyOnClick('.c1', clickHandler1)
S.bodyOnClick('#c2', clickHandler2)
S.bodyOnClick('[data-c3]', clickHandler3)

function clickHandler1() { console.log('You clicked on Class: ', this) }
function clickHandler2() { console.log('You clicked on ID: ', this) }
function clickHandler3() { console.log('You clicked on DataAttribute: ', this) }

HTML (Chainable)

Change html inside selector. Data can be text or HTML-like text with tags

S.html(selector, data)
S.html(this, 'I am awesome')
S.html(this, '<h1>I am awesome</h1>')

Append / Prepend (Chainable)

Appends / prepends data inside selector. Data can be text or HTML-like text with tags

S.append(selector, data)
S.append(this, 'I am awesome')
S.prepend(this, '<h1>I am awesome</h1>')

Remove

Removes all selected elements from DOM

S.remove(selector)
S.remove('button')

Next / Prev

Returns First elements after / before selector. Works with first founded selector. If nothing found - returns null

S.next(selector) // <element>...</element>
S.prev(selector)
S.on('button', 'click', function () {
    console.log( S.next(this) ); // <element>...</element>
})

Next All / Prev All

Returns array of all elements after / before selector. Works with first founded selector. If nothing found - returns null

S.nextAll(selector) // [element, element, element, ...]
S.prevAll(selector)
S.on('button', 'click', function () {
    console.log( S.nextAll(this) ); // [element, element, element, ...]
})

Index

Returns index of element in parent. Second argument must be string. If second argument is passed, returns element index matched passed argument.

S.index(selector, [string])
S.index('button') // 0, 1, 2...

Siblings

Returns siblings of selector in parent. Second argument must be string. If second argument is passed, returns array of elements matched passed argument.

S.siblings(selector, [string])
S.siblings('button') // [button, button, ...]

Is

Returns true or false

S.is(selector, nodeType|string)
S.is('button', '[data-index="0"]') // true or false

Closest

Returns closest target|parent element of selector

S.closest(selector, target)
S.closest('button', '.parent') // <div class="parent">...</div>

Attr

Gets or Sets attribute to selector

S.attr(selector, attribute, [data])
// Set attribute
S.attr('button', 'data-count', '0')

// Get attribute
S.attr('button', 'data-count') // '0'

Prop

Gets or Sets property to selector

S.attr(selector, prop, [value])
// Set property
S.prop('input', 'checked', true)

// Get property
S.prop('input', 'checked') // true

Val

Gets or Sets value of input/textarea

S.attr(selector, data)
// Set value
S.val('input', 'I am awesome')

// Get value
S.val('input', 'checked') // 'I am awesome'

Slide Open / Close / Toggle

Triggers slide animation

S.slideOpen(selector, duration)
let isOpen = false;

S.on('button', 'click', function(){
    if (isOpen) {
        S.slideClose('.container', .2)
    } else {
        S.slideOpen('.container')
    }
    
    isOpen = !isOpen
})

// Or
S.on('button', 'click', function(){
    S.slideToggle('.container', .4)
})

Make Global

Sets function to global window.App object

S.makeGlobal(fnName, fn)
// IIFE
(function(){
    let startCoding = function(){
        console.log('I am awesome!')
    }

    S.makeGlobal('startCoding', startCoding)
}())

window.App.startCoding() // I am awesome!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published