-1

I believe the remove function is really synchronous though, which is why this is so confusing.

I have a list of divs with a span3 class. The divs can be removed. The first occurrence of a div with span3 must always have the first class

If the div that currently has the first class is removed, by calling:

$('#'+id).remove();

and the first is reassigned by calling:

$('.span3').first().addClass('first');

Why does the first occurence of .span3 only sometimes get assigned the first class?

The full snippet:

$.get('someFile.php', function(data) {              
    $('#'+id).remove();
    $('.span3').first().addClass('first');
});
Autonomy
  • 392
  • 2
  • 13
  • If it's easy to reproduce - check what `$('.span3')` returns in debugger – zerkms Aug 05 '13 at 03:27
  • Yep, it correctly selects the right element when I run it in console, every time I've tried anyway. – Autonomy Aug 05 '13 at 03:32
  • 2
    Double check then that `$('#'+id)` and `$('.span3').first()` select desired elements – zerkms Aug 05 '13 at 03:37
  • Yes they do, but the element that is selected also has the span3 class, and is the first span3 class. When I execute in console it works, but when I execute in that callback it doesn't always work. – Autonomy Aug 05 '13 at 14:02
  • The simple workaround could be to use `'.span3:not(#'+id+')'` selector, since I'm not sure how what you're explaining is possible. What browser do you use? – zerkms Aug 05 '13 at 21:10
  • I'm in Chrome. The workaround didn't work either, here is my console output: http://pastebin.com/FK75iGse As you can see, the identical code works in the console, but not after the callback. The callback successfully remove the element but did not assign the `first` class to the new first `span3`. I'm really confused, I'm probably missing something very obvious. – Autonomy Aug 05 '13 at 21:47
  • 1
    `68` **is not** a valid `id` attribute value http://stackoverflow.com/a/79022/251311 – zerkms Aug 05 '13 at 21:48
  • Oh wow! Very interesting, thanks! Will try with that fixed and report back. – Autonomy Aug 05 '13 at 21:50
  • That was it, fascinating! Thanks for that, wouldn't have found that for a while. – Autonomy Aug 05 '13 at 21:56

1 Answers1

1

68 isn't a valid id attribute value.

More details at https://stackoverflow.com/a/79022/251311

Community
  • 1
  • 1
zerkms
  • 230,357
  • 57
  • 408
  • 498