最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to remove label in Jquery - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share asked Dec 14, 2017 at 11:32 NeoCoderNeoCoder 1011 silver badge8 bronze badges 3
  • This code works just fine. Are you sure you have included jQuery? – Master Yoda Commented Dec 14, 2017 at 11:34
  • Your code works fine: jsfiddle/bxddmfj4. Please check the console for errors. – Rory McCrossan Commented Dec 14, 2017 at 11:35
  • Thank you guys. There was an error referencing the jquery library. Thanks again. Now it is solved. – NeoCoder Commented Dec 16, 2017 at 5:36
Add a ment  | 

3 Answers 3

Reset to default 6

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

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

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();

Adding answer to better clarify my ment.

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./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.

发布评论

评论列表(0)

  1. 暂无评论