2

I am trying to pass multiple numbers for objects for a certain class. here a preview of the piece I am trying to work on

group: function(number) {
      for(var i=0;i<number.length;i++){
         groups=number[i];
    $('.group')[groups].remove();
     }
   }

I am trying to make it so its like

code.destroy.group(0,1,2);

What this will do will remove $('.group')[0] $('.group')[1] and $('.group')[2]

I have to be doing this wrong or else this would be working correctly. Can someone lead me on the right path here.

Thanks to Felix King for leading me to the right path:

All I had to do is use the arguments parameter

group: function() {
    for(var i=0;i<arguments.length;i++){
        $('.sceditor-group')[arguments[i]].remove();
    }
  }
EasyBB
  • 6,176
  • 9
  • 47
  • 77
  • I will take a look at this Felix, it is always a pleasure seeing you here no matter the circumstances as you always lead me to the right path. And hopefully this is the right path. – EasyBB Jul 10 '13 at 14:32
  • @FelixKling if you'd like to add an answer so I can upvote, it was such a huge help! Thank you so much! – EasyBB Jul 10 '13 at 14:35
  • I'm glad I could help :) Just upvote the answer in the other question if it helped you. – Felix Kling Jul 10 '13 at 14:39
  • Oh I upvoted it :D Such an easy thing and I completely over looked it all – EasyBB Jul 10 '13 at 14:41

2 Answers2

2

jQuery objects are array-like objects that contain raw DOM elements.

Therefore, $(...)[0] is a raw DOM element, not a jQuery object.

To get a jQuery object for a specific element, call .eq().


EDIT: You're also looking for the special arguments array-like object, which you can loop through to get all of the arguments passed to your function.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • The above code works for one argument, but for multiple it doesn't so I have to be some what on the right path no. – EasyBB Jul 10 '13 at 14:24
  • @EasyBB: What do you mean? – SLaks Jul 10 '13 at 14:26
  • @EasyBB: It sounds like you're actually asking for the `arguments` variable. – SLaks Jul 10 '13 at 14:28
  • Yes that is what I mean I am sorry, the argument within the function. basically I'd like to make it so the user can type (0,1,2) or whatever number and then it will remove those corresponding elements in the raw DOM. – EasyBB Jul 10 '13 at 14:30
1

There's a problem with your logic, and that is that since you remove an element of the matching set on each iteration of your cycle, you won't remove the initial elements with index 0,1 & 2 (in that case you'd end up removing the elements 0, 2 & 4 of the original set) . Also you're calling $('.group') on each iteration which is not optimal, instead is better if you take advantage of caching the variable, and then add the element you need one by one, and then you can just call remove on the resulting set.

You could change your function to:

group: function(number) {
      var $current = $();
      var $group = $('.group');
      for(var i=0;i<number.length;i++){
         $current = $current.add($group.eq(number[i]));
      }
      $current.remove();
}

EDIT: Considering SLaks answer and the usage of arguments instead of expecting an array, you could do it like this:

group: function() {
      var $current = $();
      var $group = $('.group');
      for(var i=0;i<arguments.length;i++){
         $current = $current.add($group.eq(arguments[i]));
      }
      $current.remove();
}
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
DarkAjax
  • 15,955
  • 11
  • 53
  • 65
  • I will use this, so basically you are saying the arguments parameter is not good then – EasyBB Jul 10 '13 at 14:40
  • 2
    You could also select all `.group` elements before the loop (instead of selecting them inside the loop). Then all you have to do is `$current = $current.add($groups[arguments[i]]);` – Felix Kling Jul 10 '13 at 14:43
  • 1
    @EasyBB I think `arguments` is good, see my updated answer... – DarkAjax Jul 10 '13 at 14:44
  • @FelixKling indeed good suggestion, I just updated the answer accordingly :) – DarkAjax Jul 10 '13 at 14:48