10 examples of 'jquery find element with data attribute' in JavaScript

Every line of 'jquery find element with 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
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}
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

275function getDataAttribute( element, attribute ) {
276 if ( supportsDataset ) {
277 return element.dataset[attribute];
278 }
279 else {
280 return element.getAttribute( 'data-' + attribute );
281 }
282}
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}
41public getElementDataAttributeKeyValue(element: Cheerio, { name, key }: DataAttribute): string | null {
42 const value = this.$(element).data(name)
43
44 if (name !== undefined && key !== undefined ) {
45 try {
46 return value[key]
47 } catch (e) {
48 return null
49 }
50 }
51
52 return value
53}
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}
491getData(el, name: string): string { return this.getAttribute(el, 'data-' + name); }
73function extractElementData(element) {
74 if (!element) {
75 return {
76 eerr: 'Input element absent'
77 };
78 }
79 return {
80 etag: nn(element.tagName && element.tagName.toLowerCase()),
81 etype: nn(element.getAttribute('type')),
82 ename: nn(element.getAttribute('name')),
83 eph: nn(element.getAttribute('placeholder')),
84 ecl: nn(element.className),
85 eid: nn(element.id)
86 };
87}
144function data (node, attr) {
145 return node.getAttribute('data-' + attr)
146}
126function data(elm, name, value) {
127
128 if (value === undefined) {
129 if (typeof name === "object") {
130 for (var dataAttrName in name) {
131 _setData(elm, dataAttrName, name[dataAttrName]);
132 }
133 return this;
134 } else {
135 return _getData(elm, name);
136 }
137 } else {
138 _setData(elm, name, value);
139 return this;
140 }
141}
205function getData($element) {
206 return $element.data(namespace);
207}

Related snippets