1

I have an array and a div:

var urls = ["google", "yahoo", "facebook"];

<div class="yahoo"></div>

Is it possible to get the array index of whichever element is loaded as the div's class?

I have tried:

alert(urls.index());

and my page breaks.

2 Answers 2

8

You can try $.inArray utility function.

var urls = ["google", "yahoo", "facebook"];    
var cls = $('div').attr('class');
var ind = $.inArray(cls, urls);
Sign up to request clarification or add additional context in comments.

Comments

1
var urls = ["google", "yahoo", "facebook"];

var pos = getPosition(urls, 'yahoo'); //returns 1

function getPosition(arrayName,arrayItem) {
    for(var i=0;i<arrayName.length;i++){ 
        if(arrayName[i]==arrayItem)
        return i;
    }
}​

FIDDLE

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.