-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathrequiredField.js
More file actions
37 lines (31 loc) · 1.14 KB
/
requiredField.js
File metadata and controls
37 lines (31 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import getConstant from './constants';
import { isObject } from './isType';
var asterisk;
export const addAsterisk = (el) => {
const target = $(el);
if (isObject(target)) {
// If the element is part of a Fieldset then place the asterisk before the <fieldset><legend>
if (target.closest('fieldset').length > 0 && target.closest('fieldset').find('legend').length > 0) {
const legend = target.closest('fieldset').find('legend');
legend.html(`${asterisk} ${legend.html()}`);
// If the element is a radio button or checkbox place the asterisk after the label
} else if (target.is('[input="checkbox"]') || target.is('[input="radio"]')) {
target.after(asterisk);
// Else place the asterisk before the corresponding label
} else {
const label = target.prev();
if (isObject(label)) {
$(label[0]).before(asterisk);
}
}
}
};
export const addAsterisks = (el) => {
$(el).find('[aria-required=true]').each((idx, jqObject) => {
addAsterisk(jqObject);
});
};
$(() => {
asterisk = `<span class="red" title="${getConstant('REQUIRED_FIELD_TEXT')}">* </span>`;
addAsterisks('body');
});