I am trying to add an asterisk to a label text of an input, if the input filed is required
. I used this answer and referred this answer and I can get the desired result using the following code
jQuery(function() {
jQuery("[required]").before(jQuery("<span>", {
class: "required"
}).html("*"));
});
However, I do not want the asterisk to be added for input fields where the label texts are not available.
Look at the following where a label text is not available;
<label>
<span class="required">*</span>
<input type="text" class="form-control" id="perm_city" name="perm_city" required="required" />
</label>
as opposed to the following where a label text is available
<label for="clientid">Client ID Number
<span class="required">*</span>
<input type="text" class="form-control" id="clientid" name="clientid" required="required" />
</label>
Both has the asterisk added before the input element by my jQuery script. But the desired output is as follows;
Can anyone help me achieve this? TIA