Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 377
  • Last Modified:

jquery errorPlacement

I want to be able to append the error message to the label for each input.  I also would like to be able to do that to every label and input in my form.  I tried this code but it does not work.  It appends all the error messages to one label.  New to all this ugh!
Thanks

<script>
  $(document).ready(function(){
								 
    $("#commentForm").validate( {

	  errorPlacement: function (error, element) {
             
			 $('#name_label').append(error); 
			 $('#email_label').append(error);
         }           
	     });
  
  });
  </script>

<label for="name" id="name_label">Name</label>
     <em>*</em><input id="name" name="name" size="25" class="required"  />
   
     <label for="email" id="email_label">E-Mail</label>
     <em>*</em><input id="email" name="email" size="25"  class="email required" />

Open in new window

0
Luey
Asked:
Luey
  • 2
1 Solution
 
ansudhindraCommented:
what does the argument "element" contains in your function "errorPlacement"?
is it actual id of the element, or the element (DOM object) itself?
0
 
ansudhindraCommented:
if it contains id of the label field
then replace  $('#name_label').append(error); with $('#'+element).append(error);
0
 
Michel PlungjanIT ExpertCommented:
$(document).ready(function(){
  $("#commentForm").validate( {
    errorPlacement: function (error, element) {
      $('#'+element.attr("id")+'_label').append(error);
    }          
  });
});

but what when the error goes away?
0
 
LueyAuthor Commented:
This works great I just wish I understood it better.
I do not understand your question "But what when the error goes away?"
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now