You can shorten this as much:
$(document).ready(function() {
$('#fromDate, #toDate').click(function() {
$(this).addClass('visited');
});
});
function apply() {
var FirstName = $('#name').val();
$('#name').toggleClass("error", FirstName == "");
$("#fromDate").toggleClass("error", !$('#fromDate').hasClass('visited'));
$("#toDate").toggleClass("error", !$('#toDate').hasClass('visited'));
}
Or you can pass this
in the function args:
<button class="send" onclick="apply(this)">Send</button>
now in the function:
function apply($this) {
var $els = $this.siblings('input');
$els.each(function(){
$(this).toggleClass("error", $(this).val() == "");
});
}
solved Jquery Form Validation