custom-jquery-methods
🇺🇸 English | 🇷🇺 Русский язык
Custom jQuery methods ($.fn)
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
$.fn.nodeName()
$.fn.addClassSiblingsRemove()
$.fn.changeMyClass()
$.fn.getMyElements()
$.fn.hasInitedKey()
$.fn.removeInitedKey()
You can include all methods by one file
nodejs:
// es6;// or minimised version; // es5;// or minimised version;
browser / download:
- https://unpkg.com/custom-jquery-methods@latest/dist/index.js
- https://unpkg.com/custom-jquery-methods@latest/dist/index.min.js
$.fn.nodeName ()
Returns nodeName
property of DOM element in lowercase.
Useful when you can get unknown elements.
Examples:
{ }
nodejs:
// es6;// or minimised version; // es5;// or minimised version;
browser / download:
- https://unpkg.com/custom-jquery-methods@latest/dist/node-name.js
- https://unpkg.com/custom-jquery-methods@latest/dist/node-name.min.js
$.fn.addClassSiblingsRemove (cssClass [, customPath])
Adding a class to the current element and deleting from adjacent elements
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
cssClass |
string |
The class to be added | |
customPath |
string[] |
<optional> |
Custom path to adjacent elements |
Examples:
example 1. On the same level
; // it will reproduce;
example 2. At the level of the parent elements with customPath
parameter
; // it will reproduceparent;
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; else if condition2 customPath = 'next'; else if condition3 customPath = 'prev';; // it will reproduceif condition1 parentparent; else if condition2 next; else if condition3 ; else parent;
nodejs:
// es6;// or minimised version; // es5;// or minimised version;
browser / download:
- https://unpkg.com/custom-jquery-methods@latest/dist/add-class-siblings-remove.js
- https://unpkg.com/custom-jquery-methods@latest/dist/add-class-siblings-remove.min.js
$.fn.changeMyClass (condition, previouslyAdded, className[, onChange])
Add className
if previously was removed.
Remove className
if previously was added.
Returns boolean
- className was added or not!
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
needToAdd |
boolean |
add className ? | |
previouslyAdded |
boolean |
was className previously added or not | |
className |
string / string[] / function |
see api.jquery.com/addClass and api.jquery.com/removeClass | |
onChange |
function |
<optional> |
Usage example:
/** * @param * @param */ { const $window = ; let previouslyAdded = $section; // <-- $window;}
nodejs:
// es6;// or minimised version; // es5;// or minimised version;
browser / download:
- https://unpkg.com/custom-jquery-methods@latest/dist/change-my-class.js
- https://unpkg.com/custom-jquery-methods@latest/dist/change-my-class.min.js
$.fn.getMyElements (dataKey, selector [, direction][, notSelf])
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:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
dataKey |
string |
the property key from the data object of the element | ||
selector |
JQuery.Selector |
search selector | ||
direction |
string/JQuery |
<optional> |
"document" |
direction where to look for - [closest, parent, children, find, prev, next, siblings] , or can be jQuery element for find selector inside |
notSelf |
boolean |
<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 = ;
example 2. Find / retrieve nested items only in context block
let $context = ;let $els = ;
example 3. Finding / getting the parent element
let $wrapper = ;
example 4. Search / retrieve similar items except for the current one
let $sameEls = ;
nodejs:
// es6;// or minimised version; // es5;// or minimised version;
browser / download:
- https://unpkg.com/custom-jquery-methods@latest/dist/get-my-elements.js
- https://unpkg.com/custom-jquery-methods@latest/dist/get-my-elements.min.js
$.fn.hasInitedKey (key [, setKey])
Check for the existence of an initialization key in .data()
.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
key |
string |
key name | ||
setKey |
boolean |
<optional> |
true |
set the key, if not exist |
Returns:
boolean
Examples:
example 1. Check in cycle .each()
let initKey = 'my-key';;
example 2. Check for single element
let initKey = 'my-key';let $myEl = ;if !$myEl // process current element
nodejs:
// es6;// or minimised version; // es5;// or minimised version;
browser / download:
- https://unpkg.com/custom-jquery-methods@latest/dist/has-inited-key.js
- https://unpkg.com/custom-jquery-methods@latest/dist/has-inited-key.min.js
$.fn.removeInitedKey (key)
Removing an initialization key in .data()
.
The method is the inverse action for $.fn.hasInitedKey
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
key |
string |
имя ключа |
Returns:
jQuery.<Element>
- this
Examples:
let initKey = 'my-key';let $myEl = ;$myEl;
nodejs / browser / download:
The method is described in the same file as $.fn.hasInitedKey, accordingly, you get both methods.