.hasData(key)
Available on: InDomReturns true if the underlying element has a data-* attribute or in its internal memory map, otherwise false.
Available only for objects whose underlying element is connected to the DOM, ensuring internal state consistency.Parameters:
Available only for objects whose underlying element is connected to the DOM, ensuring internal state consistency.Parameters:
key {any} - Data key to test (automatically stringified and prefixed with data- for attribute check)
Returns: {boolean} - true when the key exists, false otherwiseThrows: Error - If the underlying element has been removed
Examples:const div = $1('.example>div');
// click-counter stored in memory
div.onClick(() => {
// read counter (default 0 if never stored)
const clicks = div.getData('clicked') ?? 0;
div.setData('clicked', clicks + 1);
});
// store object and int
div.setData('user', { id: 34, name: 'Bob' });
console.log(div.hasData('user') ? 'has data key: user'
: 'doesn\'t have data key: user');
//has data key: user
div.setData('test', 1);
console.log([div.getData('user'), div.getData('test')]); // [{id: 34, name: 'Bob'}, 1]
// remove only 'user'
div.removeData('user');
console.log([div.getData('user'), div.getData('test')]); // [undefined, 1]
// grab every editable field inside the first .input-examples
const fields = $a('input, textarea, select', $1('.input-examples'));
// snapshot original values as JSON strings
fields.each(n => n.setData('originalValue', JSON.stringify(n.getValue())));
// button to check if anything changed
$1('body').append('Any field modified?');
$1('.checkBtn').onClick(() => {
// InDomArray extends Array and inherits all standard array methods.
// true if any field's current value differs from its snapshot (stops at first true)
const modified = fields.some(n =>
n.getData('originalValue') !== JSON.stringify(n.getValue())
);
console.log(modified ? 'modified' : 'same');
});
{
// The above is to demonstrate different concepts because with InDom you could just:
const section = $1('.input-examples');
$1('body').append('Any field modified? (rw)');
const original = JSON.stringify($v(section));
$1('.rwCheckBtn').onClick(() => console.log(
original === JSON.stringify($v(section)) ? 'same' : 'modified'
));
}Next: removeData »