Link to home
Start Free TrialLog in
Avatar of Luey
LueyFlag for United States of America

asked on

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

Avatar of Sudhindra A N
Sudhindra A N
Flag of India image

what does the argument "element" contains in your function "errorPlacement"?
is it actual id of the element, or the element (DOM object) itself?
if it contains id of the label field
then replace  $('#name_label').append(error); with $('#'+element).append(error);
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Luey

ASKER

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