I'm using the tooltipster plugin to show fields's hints and also another tooltip at bottom to show errors from jquery validate plugin, but for some reason all the messages updates are happening on the first error tooltip.
What am I missing?
$(document).ready(function () {
// initialize tooltipster on text input elements
$('input').tooltipster();
var tooltipsterErrorMsg = [];
// initialize validate plugin on the form
$('#myform').validate({
showErrors: function(errorMap, errorList) {
// Clean up any tooltips for valid elements
$.each(this.validElements(), function (index, element) {
if (index < tooltipsterErrorMsg.length) {
tooltipsterErrorMsg[index].hide();
}
});
// Create new tooltips for invalid elements
$.each(errorList, function (index, error) {
if (index === tooltipsterErrorMsg.length) {
var tooltipObject = $(error.element).tooltipster({
trigger: 'custom',
multiple: true,
position: 'bottom'
});
tooltipsterErrorMsg.push(tooltipObject[0]);
}
tooltipsterErrorMsg[index].content(errorList[index].message).show();
});
},
rules: {
field1: {
required: true,
email: true
},
field2: {
required: true,
minlength: 5
}
},
submitHandler: function (form) { // for demo
alert('valid form');
return false;
}
});
});
#myform {
margin: 100px;
}
input {
margin: 20px;
}
a {
display: block;
position: absolute;
bottom: 0;
}
.error {
color: #900;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/tooltipster/3.3.0/css/tooltipster.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tooltipster/3.3.0/js/jquery.tooltipster.min.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<form id="myform">
<input type="text" name="field1" title="Tooltip teste 1" />
<input type="text" name="field2" title="Tooltip teste 2" />
<br/>
<input type="submit" />
</form>
Here's the fiddle: http://jsfiddle.net/macmessa/m368ntxw/