Socket
Socket
Sign inDemoInstall

custom-jquery-methods

Package Overview
Dependencies
0
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    custom-jquery-methods

Custom jQuery methods ($.fn)


Version published
Weekly downloads
17
increased by142.86%
Maintainers
1
Install size
34.0 kB
Created
Weekly downloads
 

Readme

Source

custom-jquery-methods

node es2015 types license npm Build Status

:us: English | :ru: Русский язык

Custom jQuery methods ($.fn)

js happiness style

Basically, methods are intended for use with the webpack bundler system, there is also the ability to connect methods via direct links in browser or download them (see description of each method)

Installations

npm i --save custom-jquery-methods

# or using yarn cli
yarn add custom-jquery-methods

Methods

methods list

You can include all methods by one file

nodejs:
// es6
import 'custom-jquery-methods';
// or minimised version
import 'custom-jquery-methods/fn/index.min';

// es5
require('custom-jquery-methods');
// or minimised version
require('custom-jquery-methods/dist/index.min');
browser / download:

$.fn.nodeName ()

↑ methods list

Returns nodeName property of DOM element in lowercase.
Useful when you can get unknown elements.

Examples:
function myFunction ($el) {
    switch ($el.nodeName()) {
        case 'img':
            // ...
            break;
        case 'div':
            // ...
            break;
        case 'input':
            // ...
            break;
        default:
            // ...
    }
}
nodejs:
// es6
import 'custom-jquery-methods/fn/node-name';
// or minimised version
import 'custom-jquery-methods/fn/node-name.min';

// es5
require('custom-jquery-methods/dist/node-name');
// or minimised version
require('custom-jquery-methods/dist/node-name.min');
browser / download:

$.fn.addClassSiblingsRemove (cssClass [, customPath])

↑ methods list

Adding a class to the current element and deleting from adjacent elements

Parameters:
NameTypeAttributesDescription
cssClassstringThe class to be added
customPathstring[]<optional>Custom path to adjacent elements
Examples:

example 1. On the same level

$('.item').addClassSiblingsRemove('is-active');

// it will reproduce
$('.item').addClass('is-active').siblings().removeClass('is-active');

example 2. At the level of the parent elements with customPath parameter

$('.item').addClassSiblingsRemove('is-active', ['parent', 'siblings', 'children']);

// it will reproduce
$('.item').addClass('is-active').parent().siblings().children().removeClass('is-active');

example 3. Also, customPath allows you to use a dynamically calculated path depending on the conditions you need

let customPath = ['parent', 'siblings', 'children'];
if (condition1) {
    customPath.unshift('parent');
} else if (condition2) {
    customPath = ['next'];
} else if (condition3) {
    customPath = ['prev'];
}
$('.item').addClassSiblingsRemove('is-active', customPath);

// it will reproduce
if (condition1) {
    $('.item').addClass('is-active').parent().parent().siblings().children().removeClass('is-active');
} else if (condition2) {
    $('.item').addClass('is-active').next().removeClass('is-active');
} else if (condition3) {
    $('.item').addClass('is-active').prev().removeClass('is-active');
} else {
    $('.item').addClass('is-active').parent().siblings().children().removeClass('is-active');
}
nodejs:
// es6
import 'custom-jquery-methods/fn/add-class-siblings-remove';
// or minimised version
import 'custom-jquery-methods/fn/add-class-siblings-remove.min';

// es5
require('custom-jquery-methods/dist/add-class-siblings-remove');
// or minimised version
require('custom-jquery-methods/dist/add-class-siblings-remove.min');
browser / download:

$.fn.changeMyClass (condition, previouslyAdded, className[, onChange])

↑ methods list

Add className if previously was removed.
Remove className if previously was added.

Returns boolean - className was added or not!

Parameters:
NameTypeAttributesDescription
needToAddbooleanadd className ?
previouslyAddedbooleanwas className previously added or not
classNamestring / string[] / functionsee api.jquery.com/addClass and api.jquery.com/removeClass
onChangefunction<optional>
Usage example:
/**
 * @param {jQuery} $section
 * @param {jQuery} $frontier
 */
function showSection ($section, $frontier) {
    const $window = $(window);
    let previouslyAdded = $section.hasClass('show'); // <--
    $window.on('scroll', () => {
        const top = $window.scrollTop();
        const height = $frontier.offset().top + $frontier.outerHeight();
        previouslyAdded = $section.changeMyClass(top > height, previouslyAdded, 'show');  // <--
    });
}
nodejs:
// es6
import 'custom-jquery-methods/fn/change-my-class';
// or minimised version
import 'custom-jquery-methods/fn/change-my-class.min';

// es5
require('custom-jquery-methods/dist/change-my-class');
// or minimised version
require('custom-jquery-methods/dist/change-my-class.min');
browser / download:

$.fn.getMyElements (dataKey, selector [, direction][, notSelf])

↑ methods list

Search on the page or retrieve from the data of the desired item.

First, look at the data object on a certain property.
If it is empty - look for the element on the specified selector in the given direction. When found, write it in the data object to at subsequent calls - we get from the data, faster and without searching.

!!! If called element more then one,
then the method is performed only for the first

Parameters:
NameTypeAttributesDefaultDescription
dataKeystringthe property key from the data object of the element
selectorJQuery.Selectorsearch selector
directionstring/JQuery<optional>"document"direction where to look for - [closest, parent, children, find, prev, next, siblings], or can be jQuery element for find selector inside
notSelfboolean<optional>ignore the current element, when searching for elements, for example in document using the same selector as the current element
Returns:

jQuery.<Element> | undefined

Examples:

example 1. Find / retrieve nested items

let $els = $('.wrapper').getMyElements('$myEls', '.els-selector', 'find');

example 2. Find / retrieve nested items only in context block

let $context = $('.demo');
let $els = $('.wrapper').getMyElements('$myEls', '.els-selector', $context);

example 3. Finding / getting the parent element

let $wrapper = $('.els').getMyElements('$myWrapper', '.wrapper-selector', 'closest');

example 4. Search / retrieve similar items except for the current one

let $sameEls = $('.els').getMyElements('$mySameEls', '.els', 'document', true);
nodejs:
// es6
import 'custom-jquery-methods/fn/get-my-elements';
// or minimised version
import 'custom-jquery-methods/fn/get-my-elements.min';

// es5
require('custom-jquery-methods/dist/get-my-elements');
// or minimised version
require('custom-jquery-methods/dist/get-my-elements.min');
browser / download:

$.fn.hasInitedKey (key [, setKey])

↑ methods list

Check for the existence of an initialization key in .data().

Parameters:
NameTypeAttributesDefaultDescription
keystringkey name
setKeyboolean<optional>trueset the key, if not exist
Returns:

boolean

Examples:

example 1. Check in cycle .each()

let initKey = 'my-key';
$('.my-elements').each((i, el) => {
    let $element = $(el);
    if ($element.hasInitedKey(initKey)) {
        return true;
    }
    // process current element
});

example 2. Check for single element

let initKey = 'my-key';
let $myEl = $('.my-elements');
if (!$myEl.hasInitedKey(initKey)) {
    // process current element
}
nodejs:
// es6
import 'custom-jquery-methods/fn/has-inited-key';
// or minimised version
import 'custom-jquery-methods/fn/has-inited-key.min';

// es5
require('custom-jquery-methods/dist/has-inited-key');
// or minimised version
require('custom-jquery-methods/dist/has-inited-key.min');
browser / download:

$.fn.removeInitedKey (key)

↑ methods list

Removing an initialization key in .data(). The method is the inverse action for $.fn.hasInitedKey

Parameters:
NameTypeAttributesDefaultDescription
keystringимя ключа
Returns:

jQuery.<Element> - this

Examples:
let initKey = 'my-key';
let $myEl = $('.my-elements');
$myEl.removeInitedKey(initKey);
nodejs / browser / download:

The method is described in the same file as $.fn.hasInitedKey, accordingly, you get both methods.


Project Info

Keywords

FAQs

Last updated on 14 Jan 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc