NeoCoder
NeoCoder

Reputation: 101

How to remove label in Jquery

I have added a label in Jquery

 $('[id$="refAprtyAppId"]').after('<label class="error" id="refAprtyAppIdError">Error: Referral Id is required.</label>');

I have tried

$('[id$="refAprtyAppId"]').parent().find("label#refAprtyAppIdError").remove();

to remove the label, but it is failed to remove.

Html is

 <div class="area">
   <input id="refAprtyAppId" value="" styleClass="externalAppId referralId"/>
 </div>

Whats the problem here?

Upvotes: 0

Views: 3968

Answers (3)

Keshari Nandan
Keshari Nandan

Reputation: 1148

As per the html documentation you are allowed to have multiple id with same name on the same page. If you have multiple "error label" then you can use -

$('label.error').remove();

Upvotes: 1

Master Yoda
Master Yoda

Reputation: 4412

Adding answer to better clarify my comment.

Your code works fine:

$('[id$="refAprtyAppId"]').after('<label class="error" id="refAprtyAppIdError">Error: Referral Id is required.</label>');

$('#remove').on('click', function() {
  $('[id$="refAprtyAppId"]').parent().find("label#refAprtyAppIdError").remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="area">
  <input id="refAprtyAppId" value="" styleClass="externalAppId referralId" />
</div>
<button type='button' id='remove'>Remove Label</button>

Check the JavaScript console in your browser for other errors. Its possible that jQuery has not been referenced correctly.

Upvotes: 0

bipen
bipen

Reputation: 36541

Why don't you use id selector. You have an id there. Use it.

 $('#refAprtyAppIdError').remove();

Upvotes: 6

Related Questions