jQuery addClass example

Change class name on click in jQuery

by Dmitri Ganenco

HTML

<input class="date_of_birth" name="date[]" type="text" value="" />
<span class="stepbirthVal"></span>
<input class="date_of_birth" name="date[]" type="text" value="" />
<span class="stepbirthVal"></span>

JavaScript

const respMock = [{
  code: 0,
  message: "Please fill valid date of birth."
}, {
  code: 1,
  message: ""
}];

const inputs = $('.date_of_birth');

respMock.forEach(function(resp) {
  if (resp.message) {
    let errorSpan = $(inputs[resp.code]).next();
    errorSpan.text(resp.message);
  }
})