0

This title may be a little convoluted.... but here it goes.

I have this block of code. In the (I think it's called the parent) span, you can see the id is defined as qa_prdcmpr_qtyspan_0. There are 4 of those on a page (created as 0-3) to compare up to 4 products.

<span id="qa_prdcmpr_qtyspan_0" data-pa-element="compare-qty-column" name="qty-input">
   <br>
   <input class="txtInput" type="text" name="shoppingCartQty_0" id="qa_prdcmpr_qty_0"      maxlength="4" style="border:1px solid;border-color:#000 grey grey #000;width:35px">
   <span width="30px" id="qa_qty_error_1375968996697" class="error">Enter a quantity.</span>
</span>

In the top level span of this block, in the id=qa_prdcmpr_qtyspan_0. The 0 is a generated number (there is 0-3 for product compare)

The long number on the span id is a time stamp to guarantee uniqueness. Instead of writing the page as a generic product container and being able to include x containers on a page, they defined the page with 4 hard coded containers for product comparisons. That being said, I can work with that.

I have tried this:

/* Remove Error Message if applicable */
var $errorElements = $('.error');
$(this).find($errorElements).remove();

I'm in the process of trying this:

var errorNode = $('span[id^="qa_qty_error_"]').attr('id');
while (errorNode.firstChild) {
    errorNode.removeChild(errorNode.firstChild);
}

The problem with the first on is that it is removing all the "error" elements on the page if there are multiple errors. I thought I could get rid of that by using 'this' but I guess not.

Do I have to do something with parent / child nodes to get the correct one?

EDITED for clarity (an attempt)

ResourceReaper
  • 555
  • 2
  • 10
  • 27
  • 1
    I'm not sure what `this` is in your context (I'd expect it to be the containing ``), but I think you mean to use `$(this).find(".error").remove();` – Ian Aug 08 '13 at 15:41
  • Can you show more of your code? Not sure where $(this) applies in full context. I think what's happening is that $(this) catches a parent and find targets all the .error elements because of that. If $(this) would be pointing to the specific span between your 0-3 then it should only remove the error nested within in. – Henrik Aug 08 '13 at 15:49
  • @Ian - Yes but if I remove all '.error' classes that's not right, I want to only remove the '.error' class that is currently in the span id="qa_prdcmpr_qtyspan_{gen#}", but I'm not sure how to get that id. – ResourceReaper Aug 12 '13 at 13:57
  • 1
    @ResourceReaper I didn't say remove **all** `.error` classes. The code I provided was with `.find(".error")`, which means find `.error` elements **inside** of the main selector...in this case, whatever `$(this)` is. If you want to remove it for a specific span, use `$("#qa_prdcmpr_qtyspan_0").find(".error").remove();` – Ian Aug 12 '13 at 13:59
  • D'OH! You are right! – ResourceReaper Aug 12 '13 at 17:23

1 Answers1

0

I ended up finding a pc_prfix which is what is used to generate the 0-3 on the end of the id's. It was a little deceiving as I am of the mind set of descriptive and correct variable names.

$('#qa_prdcmpr_qty_' +pc_prfix).next('span.error').remove();

ResourceReaper
  • 555
  • 2
  • 10
  • 27