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();
    }
  }
4
  • 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. Commented Jul 10, 2013 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! Commented Jul 10, 2013 at 14:35
  • I'm glad I could help :) Just upvote the answer in the other question if it helped you. Commented Jul 10, 2013 at 14:39
  • Oh I upvoted it :D Such an easy thing and I completely over looked it all Commented Jul 10, 2013 at 14:41

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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: What do you mean?
@EasyBB: It sounds like you're actually asking for the arguments variable.
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.
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();
}

4 Comments

I will use this, so basically you are saying the arguments parameter is not good then
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]]);
@EasyBB I think arguments is good, see my updated answer...
@FelixKling indeed good suggestion, I just updated the answer accordingly :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.