I have created a custom directive that will automatically add an asterisk to the label of any input field marked as required
. The following is my link function with detailed comments:
// This is what the DOM structure looks like:
// <label id="label-1" for="example-1">Name:</label>
// <input id="example-1" type="text" acc-required>
function (scope, element, attrs) {
// The 'element' variable represents the input node
// Using jQuery to select the corresponding label based on the ID attribute
var label = $("label[for='" + element.attr('id') + "']");
if (label) {
// Adding an asterisk inside the label for required input fields
var abbrElement = angular.element('<abbr title="required" class="required-marker"">*</abbr>');
label.append(compile(abbrElement)(scope));
}
}
Is it considered bad practice to target labels using the input's ID attribute?