Hello (I couldn't find any similar question to this one, if you do please let me know), The goal to be achieved here is to insert html content after an element and if html content was inserted before remove it. My code right now [Partial HTML and Js]
<form method="POST" action="somePage.php">
<input type="text">
<input type="text">
<input type="submit">
</form>
<script>
$(document).ready(function (){
$("form").on("submit",function (e){
e.preventDefault();
$(this).find(":text").each(function (){
if($(this).val()==''){
$(this).after("<p class='error'>Please fill this field</p>");
}
});
});
});
</script>
The code does its job but the problem is that if the form is submited more than once the text "please fill this field" is displayed multiple times I have thought about putting a span (or div) after every input and writing the next code:
$(this).next("span").html("<p class='error'>Please fill this field</p>");
That would be a solution but the problen would be that I would have to write a span after every input I would want to validate and that would not be efficient. I'd like to know a better way to achieve this, If you could tell me that would be awesome,