views:

20

answers:

2

I know, I know, IE6 is the devil but I have no choice but to work with it on this project. When I try to remove a parent div it works in every browser except IE (hasn't been checked in other versions yet). Any idea why? - just added some html

$('a#remove-product'+i).click(function(event){
    $(this).parent('.product').remove();
    i--;
});




    <div class="product" id="product1">
        <h2>Product 1</h2>
        <div class="info-line">
        <label>Division</label>
        <p><select id="selection-1">
          <option value="">- Select a Division -</option>
          <option value="abrasives">Abrasives</option>
          <option value="tapes">Bonding, Surface Protection &amp; Tapes</option>
          <option value="packaging">Packaging</option>
        </select></p>
        <a id="remove-product1" href="#add-product" class="remove">Remove Product</a></div>
    </div>
+1  A: 

When are you running this code? IE shouldn't have any particular issue with this unless it's not in a document.ready, in which case you need to wrap it like this:

$(function() {
  $('a#remove-product'+i).click(function(event){
    $(this).parent('.product').remove();
    i--;
  });
});

Alternatively if IE doesn't think it's the parent, you may need .closest() instead of .parent() here.

In either case pasting the HTML will be much more helpful, but these are the very common issues associated with IE and not doing something that is standard, even across IE versions.

Nick Craver
Hm you're kind of right. It was wrapped so that's not an issue.I tried .closest() and it again works in most browsers, but when there are multiple instances of .product around it tries to remove two of them. Very odd.
Carson
i agree with .closest(). IE6 might represent its dom structure slightly differently, closest() will walk that tree
brad
@Carson - With `.closest()` it's trying to remove two of them? Can you post the HTML in that case? `.closest()` finds a single element, sounds like it's finding a parent that's not closed or something...you can check the HTMLs valid here: http://validator.w3.org/
Nick Craver
@Nick - It's all valid, and I can't check it because the divs are generated dynamically. Looking through it with firebug looks good though.
Carson
@Carson - The parent is `<div class="info-line">` not `<div class="product">` in your example so you do indeed need `.closest()` here, what's the result after adding that? Also you can just put `a.remove` for the selector instead of using `i` at all, this will bind to all of the links...is that the desired effect?
Nick Craver
Follow up: .closest() does work, but as I mention in a comment above, since there are more divs being generated below, it removes the following one.
Carson
@Nick - I only want to delete the div.product that the remove button is inside, which is why I use i. All other ones on the page should remain.
Carson
@Carson - That's what this would do, here's an example: http://jsfiddle.net/nick_craver/4ryQp/ Since you're going from `$(this)` you're finding it relatively, so it'll only get the parent it's in, no others.
Nick Craver
@Nick - genius. I ended up changing $('a#remove-product'+i) to $('a.remove') and it works great. IE6 is such a pain.
Carson
A: 

It may just be worth mentioning that you will avoid SOME ie issue by using bind() instead of click(), for example:

$('.someElement').bind('click', function(){
  SomeFunction(); //or any code for that matter
})
webfac