jsOMS/Model/Action/Dom/RemoveValue.js
Dennis Eichhorn 7b75ec58f7
Some checks failed
CodeQL / Analyze (javascript) (push) Has been cancelled
CI / general_module_workflow_js (push) Has been cancelled
fix permissions
2025-04-02 14:15:07 +00:00

45 lines
1.3 KiB
JavaScript

/**
* Remove value from input.
*
* @param {Object} action Action data
* @param {function} callback Callback
* @param {string} id Action element
*
* @since 1.0.0
*/
export function domRemoveValue (action, callback, id)
{
'use strict';
const e = action.base === 'self'
? (action.selector === '' || typeof action.selector === 'undefined'
? [document.getElementById(id)]
: document.getElementById(id).querySelectorAll(action.selector))
: document.querySelectorAll(action.selector);
for (const i in e) {
/** global: HTMLElement */
if (!Object.prototype.hasOwnProperty.call(e, i) || !(e[i] instanceof HTMLElement)) {
continue;
}
if (e[i].value === action.data) {
e[i].value = '';
} else {
e[i].value = e[i].value.replace(', ' + action.data + ',', ',');
if (e[i].value.startsWith(action.data + ', ')) {
e[i].value = e[i].value.substring((action.data + ', ').length);
}
if (e[i].value.endsWith(', ' + action.data)) {
e[i].value = e[i].value.substring(0, e[i].value.length - (', ' + action.data).length);
}
}
}
if (typeof callback === 'function') {
callback();
}
};