I’m trying to write a small snippet of jQuery that validates my input fields. If they don’t validate I want to write an error message to an empty <p> tag, only I can’t seem to target it correctly. Can anybody see/explain where im going wrong?
$(".nextform").click(function() {
var empty = $(this).parent().find("li input").filter(function() {
return this.value === "";
});
if(empty.length) {
$(this).find('p.feedback').html('error');
}
});
<div>
<li><input type="text" /><br /></li>
<li><input type="text" /><br /></li>
<li><input type="text" /><br /></li>
<li><p class="feedback"></p>
<input type="submit" value="next" class="next" /></li>
</div>
.nextform. Try selecting on"input.next"or, if you want to be more specific,"input[type=submit].next".<li>‘s should always be inside an<ul>or<ol>.<input>‘s should only havetype="submit"if they are inside a form and used to submit that form. If they are not, give themtype="button"..closestto find the nearest ancestor of a certain type, e.g.:$(this).closest("div").if (empty.length > 0) { ... }http://jsfiddle.net/PPvG/xG2KS/38/