0

Hi have a jQuery Validation on the form,

When user hits submit it generates an label tag as

<label for="LastName" generated="true" class="error">This field is required.</label>
<label for="FirstName" generated="true" class="error">This field is required.</label>
<label for="Email" generated="true" class="error">This field is required.</label>
<label for="DOB" generated="true" class="error">This field is required.</label>

I have a clear Button on the from when clicked it does the below stuff

$(':input','#myform')
.not(':button, :submit, :reset, :hidden')
.val('')
.removeAttr('checked')
.removeAttr('selected')
$("#DOB").removeAttr('value');

But i also want to remove the labels generated with the validation. I tried something like this

$(':input','#myform')
.not(':button, :submit, :reset, :hidden')
.val('')
.removeAttr('checked')
.removeAttr('selected')
$("#DOB").removeAttr('value') 
$( "label[for='LastName']" ).remove()
$( "label[for='FirstName']" ).remove()
$( "label[for='Email']" ).remove();

But it didn't work.

any help please?

HaBo
  • 13,999
  • 36
  • 114
  • 206

2 Answers2

3

Get them all in one go. All you need is:

$('.error').remove()
Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
0

Your code but with semicolon at the end :). End it works.

$('#remove').click(function() {

    $("label[for='DOB']" ).remove();
    $("label[for='LastName']" ).remove();
    $("label[for='FirstName']" ).remove();
    $("label[for='Email']" ).remove();

});

Code: http://jsfiddle.net/LS2bE/1/

Samich
  • 29,157
  • 6
  • 68
  • 77
  • thank you samich and Diodeus. as i said the label is auto generated with form validation. can i append or modify the data that the label is showing as "This field is required."? – HaBo Oct 07 '11 at 20:14
  • It depends on where you initializing validation. If it's ASP.NET MVC - you can use attribute on the model fields `[Required(ErrorMessage="My Error message")]`. If you initializing in js - check this: http://stackoverflow.com/questions/2457032/jquery-validation-change-default-error-message. You can find here overriding messages for all fields or for specific one. – Samich Oct 07 '11 at 20:21