0

Just to be sure I got it right...

myArray.removeClass('className');

will remove the class named "className" from ALL objects added to that array, yes?

ADDENDUM:

    <div id="parentEle">
        <div id="ele1"></div>
        <div id="ele2"></div>
    </div>

    #ele1 {
        //CSS
    }
    #ele1.className {
        //CSS
    }
    #ele2 {
        //CSS
    }
    #ele2.className {
        //CSS
    }

    var myArray = [];
    $('div','#parentEle').each(function() {
        myArray.push($(this)); 
    });

    // now to remove the class from both...
8
  • 1
    No, it'd throw a TypeError: Object [object Array] has no method 'removeClass'. Are you actually talking about jQuery instead? Commented Nov 30, 2013 at 23:26
  • oops, you're right. Yes this is for jQ, does that mean I have to use .each() or something? Commented Nov 30, 2013 at 23:30
  • Is this a "real" jQuery object or is this just an array (for example $('.class') vs [$('.class')[0], $('.class')[1])? They are two different things. Anyway, if it is actually a jQuery object it will work as expected. Commented Nov 30, 2013 at 23:31
  • Why are you asking this, instead of trying this? You could have just read the API for the removeClass() method and answered this yourself. Commented Nov 30, 2013 at 23:38
  • clarified question... Commented Nov 30, 2013 at 23:40

2 Answers 2

1

This would work if you use JQuery's selector and this is your array(JSFiddle):

var myArray = $('span');
myArray.removeClass('active');

Else, you get:

TypeError: Object [object Array] has no method 'removeClass'

However, using each(JSFiddle):

var myArray = [$('span').first(), $('span').first(), $('span').first()]; // selecting first element 3 times

$.each(myArray, function () {
    this.removeClass();
});

You can get expected results.

Update(to your needs) - JSFiddle:

$(function () { // execute when the page's loaded
    var myArray = $('#parentEle div'); // select all divs inside #parentEle element
    $.each(myArray, function () { // iterate over selected divs
        $(this).removeClass('className'); // remove class
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1

I'd suggest, given the ambiguities, and lack of clarity, in the question:

var myArray = $('#parentEle div').map(function(){
    return this;
}).get();

$.each(myArray, function(i,e){
    $(e).removeClass('className');
});

Coupled with the HTML:

<div id="parentEle">
    <div id="ele1" class="className">ele1</div>
    <div id="ele2" class="className">ele2</div>
</div>

JS Fiddle demo.

Or, in plain JavaScript:

[].forEach.call(document.querySelectorAll('.className'), function(a){
    a.classList.remove('className');
});

JS Fiddle demo.

1 Comment

Thanks. your first approach did what I was looking for

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.