10 examples of 'jquery add data attribute' in JavaScript

Every line of 'jquery add data attribute' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
5function addData(elem, name) {
6 return elem[name] || (elem[name] = {})
7}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

22function addAttribute(attribute, value) {
23 var attributes = filterUrl.getQueryVar(attribute);
24 if (!attributes) {
25 attributes = value;
26 } else {
27 if (attributes.indexOf(value) == -1) {
28 attributes += ',' + value;
29 }
30 }
31 filterUrl = filterUrl.addQueryVar(attribute, attributes);
32 return filterUrl;
33}
59function getDataAttribute(element, name) {
60 if ('dataset' in element) {
61 return element.dataset[name];
62 } else {
63 return element.getAttribute('data-' + hyphenize(name));
64 }
65}
130function datasetSet(element, attribute, value) {
131 if(element.dataset) {
132 element.dataset[attribute] = value;
133 return;
134 }
135
136 return element.setAttribute('data-' + attribute, value);
137}
275function getDataAttribute( element, attribute ) {
276 if ( supportsDataset ) {
277 return element.dataset[attribute];
278 }
279 else {
280 return element.getAttribute( 'data-' + attribute );
281 }
282}
37function setData(element, name, value) {
38 log(['setData ' + name + ': ' + value, element], 8, logGroup);
39 element.dataset[name] = value;
40}
195function data(el, prop, value) {
196 var prefix = 'data';
197 var elData = el[prefix] || {};
198 if (typeof value === 'undefined') {
199 if (el[prefix] && el[prefix][prop]) {
200 return el[prefix][prop];
201 } else {
202 var dataAttr = el.getAttribute(prefix + '-' + prop);
203 if (typeof dataAttr !== 'undefined') {
204 return dataAttr;
205 }
206 return null;
207 }
208 } else {
209 elData[prop] = value;
210 el[prefix] = elData;
211 return el;
212 }
213}
9function addData (element: HTMLElement, key: string, value?: any): HTMLElement|configuration|string|void {
10 if (value === undefined) {
11 return element && element.h5s && element.h5s.data && element.h5s.data[key]
12 } else {
13 element.h5s = element.h5s || {}
14 element.h5s.data = element.h5s.data || {}
15 element.h5s.data[key] = value
16 }
17}
44function dataAttr( el, attr ) {
45 var data = el.getAttribute( 'data-' + attr );
46
47 try { // JSON.parse可能报错
48
49 // 当data===null表示,没有此属性
50 data = data === null ? undefined : data === 'true' ? true :
51 data === 'false' ? false : data === 'null' ? null :
52
53 // 如果是数字类型,则将字符串类型转成数字类型
54 +data + '' === data ? +data :
55 /(?:\{[\s\S]*\}|\[[\s\S]*\])$/.test( data ) ?
56 JSON.parse( data ) : data;
57 } catch ( ex ) {
58 data = undefined;
59 }
60
61 return data;
62}
492setData(el, name: string, value: string) { this.setAttribute(el, 'data-' + name, value); }

Related snippets