Tsundoku
Tsundoku

Reputation: 9408

find a href with a certain value

I have an array called "selectMe" formed by a variable wich contains a string such as: 12P, 5B, 10C, etc., this is the "href" value of a hyperlink and I need to find and add the class "selected" to the ones inside this array. To break the array I have:

function selectPrevious(selections){
    // split into array
    var selectMe = selections.split(", ")
    for (var i = 0; i < selectMe.length; i++){
        $('#theater a').search(selectMe[i]).addClass('selected');
    }
}

I've tried doing find() instead of search() as well as many other iterations but still haven't been able to accomplish what I want, how can I do it?

EDIT

Using one of the answers provided here I have changed it to this:

function selectPrevious(selections){
            // split into array
            if(typeof selections !== "undefined"){
                    var selectMe = selections.split(", ");
                for (var i = 0; i < selectMe.length; i++){
                    $('#theater a[href*='+selectMe[i]+']').addClass('selected');
                }
            }
        }

I had to add the "if(typeof selections !== "undefined")" because otherwise it was throwing me errors on IE. Anyway, I still can't add the class "selected" to the values in the array, am I missing something? or did I do something wrong?

Upvotes: 0

Views: 2396

Answers (2)

Zain Shaikh
Zain Shaikh

Reputation: 6043

try this one:

function selectPrevious(selections) {
    // split into array
    var selectMe = selections.split(", ")
    for (var i = 0; i < selectMe.length; i++){
        $('#theater').find('a[href*='+selectMe[i]+']').addClass('selected');
    }
}

Upvotes: 0

Your selector for find() is wrong. And there are no search() in jQuery. Instead of $('#theater a').search(selectMe[i]) use $('#theater a[href*='+selectMe[i]+']')

Upvotes: 1

Related Questions