Dave Stein
Dave Stein

Reputation: 9316

Why is jQuery remove throwing .attr exception in IE8

I have code like this:

_removeSelection : function(e, refocus) {

  var thisObj      = e.data,
      lineItem     = $(this).closest('li'),
      templateData = lineItem.data('templateData');

  thisObj.element.trigger('beforeRemoveSelection',[templateData]);

  lineItem.remove();

  thisObj._selection_items = $(thisObj._selections).find('li');

  if ( ( refocus != 'undefined' && refocus === true ) || refocus == 'undefined') {
    thisObj.refocus();
  }

  thisObj.element.trigger('toggleSelection');
  thisObj.element.trigger('limitNotHit', [true]);
  thisObj.element.trigger('removeSelection',[templateData]);

}

Everything logs fine and it appears to execute fine. However after all my code runs I get an exception:

"Object doesn't support property or method 'getAttribute'"

So I have a loop that calls the function X times, but it only runs the function to completion once.

EDIT: The loop triggers a click on an element which runs that function. If I manually trigger the click using firebug, it still throws exception

EDIT 2: The exception is thrown at line 4550 in jquery-1.6.2.js

Doing lineItem[0].parentNode.removeChild( lineItem[0] ); also causes delayed exception. Looks like @zzzz is right about some weird cache cleanup.

This only happens when using .trigger('click'), not when actually clicking on the element

Upvotes: 2

Views: 2000

Answers (2)

Justin Jones
Justin Jones

Reputation: 91

I believe this is because IE bubbles the click event to a node that no longer exists (and then jQuery tries to do something with that?).

I assume because your click removes the parent element, so when it tries to bubble to that element it breaks.

Anyways, I have seen the exact same thing in IE when using backbone.js to re-render on change events - change event bubbles up but node no longer exists (because its been rendered again so it's a new node) and it fires this error.

Using event.stopPropagation [ http://api.jquery.com/event.stopPropagation/ ] has worked to solve this for me in the past, give it a shot.

Upvotes: 5

Baz1nga
Baz1nga

Reputation: 15579

I would like to say it's a bug. There is an unhandled exception in sizzle, it only happens in browsers not supporting querySelectorAll (like IE7 or IE8+ in compatibility mode) .

The attribute-handler for type receives an argument that is not an element-node.

If function seems to work as expected just catch and ignore this error.

add this after you import your jquery file.

<script>
 if($.browser.msie)
 {
 jQuery.find.selectors.attrHandle.type=function( elem ) {   
       try{return elem.getAttribute( "type" );}catch(e){return'';}
    }
 }
</script>

I realise its a hack.. but better than an error.

Upvotes: 0

Related Questions