1

I have a List li of elements that I used .toArray(). I now need to loop through them to find the desired element and change its style Class.

I am not sure what I am doing wrong, but I cannot seem to get the class of the index item, but I can retrieve the innerHTML no problem.

var viewsIndex = $('#viewsList li').toArray()
        for(i=0; i < viewsIndex.length; i++) {
            if(viewsIndex[i].innerHTML == selectedTab) {

                console.log(viewsIndex[i].attr('style')); //This does NOT work
                console.log(viewsIndex[i].innerHTML); //This does work
            }
            else
            {


            }
        }

Once I target the Element, I want to use .removeClass and .addClass to change the style.

4 Answers 4

5

This is the DOM object which doesn't have jQuery functions:

viewsIndex[i]

This is the jQuery object which has the attr function:

$(viewsIndex[i]).attr('style')

Anyway, your code could be a lot simpler with this:

$('#viewsList li').filter(function(){
    return this.innerHTML == selectedTab;
}).removeClass('foo').addClass('bar');
Sign up to request clarification or add additional context in comments.

5 Comments

Please use $(this).html() :-). jQuery doesn't normalize everything for you to use innerHTML :P
@FlorianMargaine, this is so wrong, jQuery wasn't meant to replace javascript(as it is javascript...), it helps you doing things easier, if you are over using jQuery- meaning you're using verbose jQuery functions instead of using js native functions, you're doing something wrong. jQuery- write less do more, isn't it? please read the "Know Your DOM Properties and Functions"
Dude. innerHTML works differently across browsers. jQuery normalizes everything for you. If you're using jQuery, at least use it when necessary --- this case is a necessary one.
@gdoron with that logic why wrap the element to use .attr() when .getAttribute() is faster, well supported and not overly verbose in comparison to the jQuery method.
@rlemon, I think getAttribute has some problems in some browsers, anyway I do use getAttribute if I know which browsers I'm targeting. with innerHTML I've never heard or had problems. see this in the source code of jQuery's attr: attrHooks
4

You are trying to call jQuery function on DOM object convert it to jQuery object first.

Change

viewsIndex[i].attr('style')

To

$(viewsIndex[i]).attr('style')

4 Comments

I just tried var $item = viewsIndex[i]; console.log($item.attr('style')); with no luck.
@Rob, well... This isn't the code he wrote in his answer... anyway, you're doing it wrong, use the filter function.
You did not convert the element to jQuery object try this, var $item = $(viewsIndex[i]); console.log($item.attr('style'));
Just saw that error on my part. thanks for the quick responses
1

couldn't you use .each()?

$('#viewLists li').each(function(i){
    if($(this).html == selectedTab){
       $(this).removeClass('foo').addClass('bar');
    }
});

Comments

0

Loop over the elements using jQuery each and then access them as $(this). This way you'll have access to jQuery methods on each item.

$('#viewsList li').each(function(){

    var element = $(this);

    if(element.html() == selectedTab){
        console.log(element.attr('style')
    } else {

    }
}

Comments

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.